How do you decode complex declarations of pointers + arrays?

Although I use it std::vectoralmost all the time, I am interested in understanding as many pointers as possible. Examples of what I'm talking about:

char* array[5]; // What does it mean?
// 1) pointer to an array of 5 elements!
// 2) an array of 5 pointers?

I am interested in the exact definition of this declaration.

+3
source share
7 answers

Not just pointers and arrays: How to interpret complex C / C ++ declarations :

Start reading the declaration with the innermost parentheses, go right, and then go left. When you encounter brackets, the direction should be the other way around. As soon as everyone in the parentheses sorted out, jumped out of this. Continue until the whole declaration has been analyzed.

: , .

:

char* array[5];

5 char.

+3

cdecl - , . ( !)

Type `help' or `?' for help
cdecl> explain char* foo[5]
declare foo as array 5 of pointer to char
cdecl> declare bar as array 5 of pointer to function (integer, integer) returning char
char (*bar[5])(int , int )
+3

C/++:

  • , typedef, const, volatile .. "char".
  • , . * (), [] () () ( ).

"char * array [5]", - "", - [] (), * ().

" , ".

" " array ", , char".

, " ", , , char "

, , [] () , *. , .

+2

char * - , 5 .

+1

[] , *, , .

+1

, "*" . char ** a [5] - 5 ...

+1
source

Source: https://habr.com/ru/post/1718957/


All Articles