交换多维结构? [英] Swap Mult Dimentional Structure?
问题描述
你好。
据网上其他地方说,结构可以换成如下。
Hello.
It is said elsewhere on the net that structures can be swapped as below.
typedef struct {
int x[5];
} intarr;
int main()
{
intarr a, b, temp;
// Swap Structure
temp = a;
a = b;
b = temp;
}
太棒了!
我可以为多维结构做同样的事吗?
我的结构是3维的。 m_tControlMatrix [128] [5] [9]
口也宣告m_tControlMatrixB [128] [5] [9]和m_tControlMatrixTemp [128] [5] [9]和
希望交换它们与上面的代码一样,但如果我这样做:
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:
m_tControlMatrixTemp = m_tControlMatrix;
我受到欢迎:错误C2106:'=':左操作数必须为l值
如果我尝试:
I am greeted with: error C2106: '=' : left operand must be l-value
If I try:
m_tControlMatrixTemp[0][0][0] = m_tControlMatrix[0][0][0];
它编译,但这只会交换[0]元素
>
我想更改结构的起始地址,以便我可以在一个上进行维护工作
下游代码可以在清理副本上工作但不知道它现在正在工作有副本。
帮助?
谢谢
:Ron
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
推荐答案
如果你想对你的(相对较大的)数组使用这个技巧,你想将结构编码为:
If you want to use this trick with your (relatively large) arrays you want to code the struct as:
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."]
这篇关于交换多维结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!