我想从 {0,1,2,3}
开始跳过 p,到 {4,5,6,7}
等等。如何我这样做?
#include <stdio.h>
int main() {
char td[6][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15},
{16, 17, 18, 19},
{20, 21, 22, 23}
};
char* p = *td;
printf("Address of td: \t%p, value=%u\n", td, (int)**td);
printf("Address of p: \t%p, value=%u\n", p, (int)*p);
p++;
/* How do I skip to the start of {4,5,6,7} (ie to be pointing at 4) ? */
printf("Address of p: \t%p, value=%u\n", p, (int)*p);
return 0;
}
最佳答案
你应该先阅读this answer: Declaration-2
:我在这里解释了如何将 char 二维数组存储在内存中。
char*
p
指向数组中的 td[0][0]
元素,即 0
。如果你想增加 p
使得 p
指向 4
即 td[1][0]
然后将 p
增加 cols = 4(数组为 row = 6 * col = 4)。
p = p + 4;
检查 this working代码。我应用了 row-major 的概念.
关于c - 如何跳过多维数组中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420849/