char*(*arr)[2]和char**数组[2]如何彼此不同?

May*_*ank 4 c c++ arrays string pointers

怎么char * (*arr)[2]char **array[2]彼此不同?如果我char* strings[2]使用函数传递,那么如何从问题的第一部分提到的方式访问元素?还请告诉其他方法来访问指针数组的元素.谢谢.

小智 12

CDecl报告:

char *(*arr)[2]

declare arr as pointer to array 2 of pointer to char
Run Code Online (Sandbox Code Playgroud)

char **arr[2]

declare arr as array 2 of pointer to pointer to char
Run Code Online (Sandbox Code Playgroud)

只是[]数组声明符的优先级高于*指针限定符,因此括号会改变含义.


P0W*_*P0W 7

遵循螺旋规则: -

a] char*(*arr)[2]

        +------+
        | +--+ |
        | |  | |
        | ^  | |
char * (*arr ) [2] 
     |  |    | |
     |  |    | |
     |  +----+ |
     +---------+

identifier arr is pointer 
            to array of 2
            pointer to char
Run Code Online (Sandbox Code Playgroud)

b] char**arr [2]

      +----------+
      | +----+   |
      | ^    |   |
char* *arr   [2] |
    | |      |   |
    | +------+   |
    +------------+


identifier arr is an array 2
            of pointers
            to pointer
            to char
Run Code Online (Sandbox Code Playgroud)

同样的,

c] char*strings [2]

                    +-----+
                    |     |
                    ^     |
            char *strings [2]
                 |        |
                 +--------+

 identifier string is an array of 2
            pointers 
            to char
Run Code Online (Sandbox Code Playgroud)

所以,知道弄清楚差异


Mik*_*our 5

char * (*arr)[2] 是指向数组的指针.

char **array[2] 是一个指针指针数组.

如果我char* strings[2]使用函数传递,那么如何从问题的第一部分提到的方式访问元素?

strings是一个指针数组,因此&strings给出第一种类型.你无法从中获得第二种类型.

在C++中,我建议你不要乱用奇怪的复合类型,并使用更高级别的类数组,例如std::vectorstd::string.