Why can't we use double pointer to represent two dimensional arrays?

前端 未结 5 1088
我在风中等你
我在风中等你 2020-11-22 02:23

Why can\'t we use double pointer to represent two dimensional arrays?

arr[2][5] = {\"hello\",\"hai\"};
**ptr = arr;

Here why doesn\'t the d

5条回答
  •  面向向阳花
    2020-11-22 02:53

    In C, a two-dimensional array is an array of arrays.

    You need a pointer-to-array to refer to it, not a double-pointer:

    char array[2][6] = {"hello", "hai"};
    char (*p)[6] = array;
    //char **x = array;  // doesn't compile.
    

    For a double pointer to refer to "2-dimensional data", it must refer to the first element of an array of pointers. But a 2-dimensional array in C (array of arrays) is not the same thing as an array of pointers, and if you just define a 2-D array, then no corresponding array of pointers exists.

    The only similarity between the two is the [][] syntax used to access the data: the data itself is structured quite differently.

提交回复
热议问题