c - C 中的指针和二维数组

标签 c arrays pointers

我知道如何在 C 中将指针与一维数组一起使用。像下面这样: 但是如果我们有二维数组呢?我如何通过指针解决它们? 谢谢。

#include <stdio.h>

int main() {
  int dar[4] = {1,2,3,4};

  int *sar = NULL;
  sar = dar;
  for (int i = 0; i < 4; i++) {
    printf("%d ", *(sar + i));
    }
}

最佳答案

这也可能有帮助。

 #include <stdio.h>

int main() 
{   
    int dar[2][3] = {1,2,3,
                    4,5,6}; 
    int index=0;
    for (int line = 0; line < 2; line++) 
    {
        for (int col=0; col<3;col++)
        {               
            printf("%d ", *(dar[0]+index));
            index=index+1;
        }
        printf("\n");
    }   
    return (0);

关于c - C 中的指针和二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724386/

相关文章:

C - 使用多线程时使用 lseek() 获得的 write() 位置不准确

c - 在使用 fmemopen 打开的 FILE* 上使用 rewind()

c - 令我困惑的递归调用

c - 如何将 token (strtok)存储在数组的指针中

css - 跨度类 "mark"中的背景颜色覆盖

c - 在函数之间传递指针

c - TLB、CPUID 和 Hugepages?

arrays - 在 Julia 中将整数数组转换为字符串数组

c++ - 在包含对象指针的数组中释放内存的正确方法

pointers - 为什么不能将指针附加到数组?