Because I couldn't find any reading that explains anything more than int(*p)[5];
I decided to ask a question here, that may also be used as a reference in use of other users, because it is a bit confusing I think.
What the heck is int*(*asex)[5];
?
I am self-taught, but also I've never seen it explained in a variety of different courses AND I never used it in practice. Right now, I need a clear explanation of that, because I am creating a C code analyzer for my project. Even though it wasn't directly explained to me, I am used to the language semantics so here is what I think it means:
int(*a)[5];
Clearly a pointer to an array of 5 ints (pointer toint[5]
)int(**asex)[5];
pointer to an array of 5 int pointers?int(*arr[10])[10];
That should be an array of 10 pointers toint[10]
int*(*asex)[5];
What is that? pointer to a pointer toint[5]
?
What did I got wrong? Additionally, I feel like the only real advantage of this is shown with pointer arithmetic, is that true?
CodePudding user response:
There is a trick to read these definitions: the spiral method.
- start from the symbol
- parse the suffix operators left to right (
[]
for arrays and()
for functions), if you reach a)
, skip to the next rule - then parse the prefix operators right to left,
*
for pointer to - when you reach a
(
skip back to step 2 for the parenthesized expression. - if you reach the final type stop.
Applying this to the examples:
int(*a)[5];
defines a pointer to arrays of 5 ints. Typically the type received by a function taking a matrix defined asint mat[4][5]
;int(**asex)[5];
defines a pointer to a pointer to arrays of 5 ints. The type of the address of the function argument defined above.int(*arr[10])[10];
defines an array of 10 pointers to arrays of 10 ints. You could store 10 pointers to matrices with 10 columns and various numbers of rows.- Finally
int*(*asex)[5];
defines a pointer to arrays of 5 pointers to int.