c : 如何从其他数组初始化多维数组?
c : How to initialize mulitdimensional arrays from other arrays?
我的 C 代码有问题:
输入:整数数组[16]
(whished)输出:array2[4][4] 所以array2[0][1]-array2[0][3][=中的前4个元素11=]
我现在得到了这样的东西,但我没有弄清楚为什么它不起作用;
我知道 array[1] = *(array + 1);
如果某人能帮助我,那就太好了。
int write_from_state(int *state[][4])
{
encryption[0] = state[0][0];
encryption[1] = state[1][0];
encryption[2] = state[2][0];
encryption[3] = state[3][0];
encryption[4] = state[0][1];
encryption[5] = state[1][1];
encryption[6] = state[2][1];
encryption[7] = state[3][1];
encryption[8] = state[0][2];
encryption[9] = state[1][2];
encryption[10] = state[2][2];
encryption[11] = state[3][2];
encryption[12] = state[0][3];
encryption[13] = state[1][3];
encryption[14] = state[2][3];
encryption[15] = state[3][3];
return encryption;
}
我收到错误
警告:赋值从整数生成指针而不进行强制转换 [-Wint-conversion]
状态[0][0] = 消息[0] ;
int *write_from_state(int *dest, int *src, size_t rowLength, size_t nRows)
{
int *tmp = dest;
while(nRows--)
{
memcpy(tmp, src, sizeof(*src) * rowLength);
tmp += rowLength;
src += rowLength;
}
return dest;
}
或者只是
memcpy(dest, src, nRows * rowLength * sizoef(*src));
甚至
int *dest = (void)src;
然后 dest[x]
;
我的 C 代码有问题:
输入:整数数组[16]
(whished)输出:array2[4][4] 所以array2[0][1]-array2[0][3][=中的前4个元素11=]
我现在得到了这样的东西,但我没有弄清楚为什么它不起作用; 我知道 array[1] = *(array + 1);
如果某人能帮助我,那就太好了。
int write_from_state(int *state[][4])
{
encryption[0] = state[0][0];
encryption[1] = state[1][0];
encryption[2] = state[2][0];
encryption[3] = state[3][0];
encryption[4] = state[0][1];
encryption[5] = state[1][1];
encryption[6] = state[2][1];
encryption[7] = state[3][1];
encryption[8] = state[0][2];
encryption[9] = state[1][2];
encryption[10] = state[2][2];
encryption[11] = state[3][2];
encryption[12] = state[0][3];
encryption[13] = state[1][3];
encryption[14] = state[2][3];
encryption[15] = state[3][3];
return encryption;
}
我收到错误
警告:赋值从整数生成指针而不进行强制转换 [-Wint-conversion] 状态[0][0] = 消息[0] ;
int *write_from_state(int *dest, int *src, size_t rowLength, size_t nRows)
{
int *tmp = dest;
while(nRows--)
{
memcpy(tmp, src, sizeof(*src) * rowLength);
tmp += rowLength;
src += rowLength;
}
return dest;
}
或者只是
memcpy(dest, src, nRows * rowLength * sizoef(*src));
甚至
int *dest = (void)src;
然后 dest[x]
;