I'm going crazy with pointers in C at the moment. I have the following two multi-dimensional arrays:
我现在对C中的指针很着迷。我有以下两个多维数组:
int num0[5][3] =
{ {0,1,0},
{1,0,1},
{0,1,0},
{1,0,1},
{0,1,0}
};
int num1[5][3] =
{ {1,1,1},
{1,0,1},
{0,1,1},
{0,1,0},
{1,0,0}
};
These are then packed into another array as such:
然后将它们打包到另一个数组中:
int (*numbers[])[3] = { num0, num1 };
If I then do:
如果我那么做:
printf( "Result: %d\n", numbers[0][2][2] );
I get the expected result, in this case Result: 1.
我得到了预期的结果,在这种情况下结果是:1。
However, I'd like to assign numbers[0] to another variable. So in a modern programming language, you'd do something as simple as:
但是,我想将数字[0]分配给另一个变量。所以在现代编程语言中,你可以做一些简单的事情:
int newvar[5][3] = numbers[0];
printf( "Result: %d\n", newvar[2][2] );
Even though my pointer knowledge is limited, I know this isn't going to work (and it of course doesn't). But for the life of me I can't figure out the correct syntax to make it work (and more importantly, understand WHY it works).
尽管我的指针知识有限,但我知道这不会起作用(当然也不会)。但对我来说,我无法找到正确的语法来使它工作(更重要的是,理解它为什么工作)。
If anyone out there can help me out here I'd really appreciate it!
如果有人能帮助我,我将非常感激!
Thanks
谢谢
1 个解决方案
#1
2
You cannot assign arrays in C, use memcpy
to copy arrays:
不能在C中分配数组,使用memcpy复制数组:
memcpy(newvar, numbers[0], sizeof newvar);
#1
2
You cannot assign arrays in C, use memcpy
to copy arrays:
不能在C中分配数组,使用memcpy复制数组:
memcpy(newvar, numbers[0], sizeof newvar);