【发布时间】:2020-12-31 05:52:08
【问题描述】:
我一直在尝试通过将二维数组作为参数传递给函数来更新它,但我似乎不明白它是如何工作的,我在下面展示了一个示例,它可以正常工作,它会更新值。
void substitution_sbox(uint8_t a[4][4]) {
int index1, index2;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
index1 = a[i][j] >> 4;
index2 = a[i][j] & 0x0f;
printf("index[1]: %x and index[2]: %x\n", index1, index2);
a[i][j] = g_aes_sbox[index2 + (16 * index1)];
// g_aes_sbox[] is a globally defined array.
}
}
}
uint8_t plain_text[4][4] = { {0x32, 0x43, 0xF6, 0xA8}, {0x88, 0x5A, 0x30, 0x8D}, {0x31, 0x31, 0x98, 0xA2}, {0xE0, 0x37, 0x07, 0x34} };
//after calling substitution from here..
// SUBSTITUTION.
printf("calling substitution\n");
substitution_sbox(plain_text);
// this function when printing, updates the array correctly.
现在,如果我尝试在另一个函数中调用相同的数组,它不会像我想的那样更新 -
void row_shift(uint8_t a[4][4]) {
for(int i=1;i<4;i++){
for(int j=0;j<4;j++){
// rotate this row by i positions, to the left.
// m = no. of rotations.
for(int m=0;m<i;m++) {
// rotate by one.
uint8_t temp = a[i][0];
for(int n=0;n<4;n++) {
a[i][n] = a[i][n+1];
}
a[i][3] = temp;
// rotate by one
}
for(int g=0;g<4;g++) printf("%04x ",a[i][g]);
printf("\n");
}
}
}
这个函数不会改变数组,数组保持不变,有人可以帮我理解更新在这两种情况下是如何工作的吗? 谢谢。
【问题讨论】:
标签: arrays c multidimensional-array