代码之家  ›  专栏  ›  技术社区  ›  Shweta

通过指针表示二维数组

c
  •  2
  • Shweta  · 技术社区  · 15 年前

    一维数组的地址实际上是

    a[i]=*(a+i);
    

    二维数组的地址是否计算为

    a[i][j]=**(a+i+j);
    
    5 回复  |  直到 15 年前
        1
  •  4
  •   Oliver Charlesworth    15 年前

    *(*(a+i)+j)
    
        2
  •  2
  •   John Bode    15 年前

    递归应用规则:

    a[i][j] == *(a[i] + j) == *(*(a + i) + j)
    
        3
  •  0
  •   user23743 user23743    15 年前

    不,因为那样的话 a[1][2] a[2][1] 在同一个地方。像这样的 *(a+i*n+j)

        4
  •  0
  •   pmg    15 年前

    a a[i] 有不同的类型(分别是 int** int* ).

    假设在你的例子中 被定义为int数组的数组(例如 a[10][20] ),将其传递给函数(从而将其转换为 pointer to the first element of the array

        a           is of type `int**`
        a[i]        is of type `int*`
        a[i][j]     is of type `int`
    
        *(a+i)      is of type `int*`
        a+i+j       is of type `int**`
        *(a+i+j)    is of type `int*`
        *(*(a+i)+j) is of type `int`
    
        5
  •  0
  •   AnthonyLambert    15 年前
    // CTest.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    void print (int i, int j )
    {
        int a[3][3] = {
            { 1,2,3 },
            { 4,5,6 },
            { 7,8,9 }
            };
        printf ("%d\n", *(*(a+i)+j) );
        printf ("%d\n", a[i][j] );
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        print (0,0);
        print (1,1);
        print (2,2);
        return 0;
    }
    

    退货:

    1 5 9

    *这是通过编译器运行的。。。。