c - 如何跳过多维数组中的行

标签 c pointers

我想从 {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 指向 4td[1][0] 然后将 p 增加 cols = 4(数组为 row = 6 * col = 4)。

p = p + 4;

检查 this working代码。我应用了 row-major 的概念.

关于c - 如何跳过多维数组中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420849/

相关文章:

c - 在 C 中,*、=、++、<< |运算符(operator)

c - 如何计算 Linux 中进程中线程使用的 CPU 百分比?

c - 如何递归遍历目录并打印 C 中的所有文件?

pointers - 递归地附加到 slice

c - 为什么我会得到这些随机值?

c - 将文件读入内存?

c - 我可以从 pic 18f4550 的 PORTBbits.RB7 看哪个值

c++如何使用指针将字符串与动态结构字符串进行比较?

c++ - 通过重新解释转换将 constexpr 值转换为指针

c - 微型 C 编译器 : "error: unknown opcode ' jmp'"