Click here to Skip to main content
15,883,750 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.

It is said elsewhere on the net that structures can be swapped as below.

C++
typedef struct {
    int x[5];
} intarr;

int main()
{
    intarr a, b, temp;

 // Swap Structure

    temp = a;
    a    = b;
    b    = temp;

}


Great!

Can I do the same for a multidimensional structure?
My structure is 3 dimensional. m_tControlMatrix[128][5][9]

I declared also m_tControlMatrixB[128][5][9] and m_tControlMatrixTemp[128][5][9] with the
desire to swap them as with the above code but if I do:

C++
m_tControlMatrixTemp = m_tControlMatrix;


I am greeted with: error C2106: '=' : left operand must be l-value

If I try:

C++
m_tControlMatrixTemp[0][0][0] = m_tControlMatrix[0][0][0];


It compiles, but this only swaps the [0] elements

I want to change the start address of the structures so I can do maintenance work on one while
the code down stream can work on a cleaned up copy but not know it is now working with a copy.

Help?

Thanks

:Ron
Posted
Updated 16-Jun-13 16:56pm
v2

1 solution

If you want to use this trick with your (relatively large) arrays you want to code the struct as:
C++
typedef struct {
    int x[128][5][9];
} My3Darr;

I think this answers your question but this solution will move fairly large amounts of memory around.

A better way to do it is to use pointers to the structs and exchange the pointers. ["left as an exercise for the student."]
 
Share this answer
 
Comments
nv3 17-Jun-13 4:54am    
My 5.
Ron Anders 17-Jun-13 9:09am    
Thanks,

Pointers to a struck was my first attempt. But I found myself editing gobs of old code where random accesses are made to the matrix causing alot of work. By the time my head was really hurting from flat pointer access to the matrix I thought maybe I could just swap the addresses and nobody would be the wiser.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900