What does char (* (* a [4]) ()) [5] mean?

Hi, I came across a question in the section "Test your skills in C ++".

Please let me know what this means with an example?

Edited section : Sorry for the extra bracket, edited and deleted.

char (*(*a[4])())[5] 
+7
source share
4 answers

I cheated by deleting what I think is an extra right bracket and inserts the result into cdecl .

declare a as array 4 of pointer to function returning pointer to array 5 of char

+16
source

Following the spiral rule (associated with chris) and starting with the identifier:

 a 

... there is...

 a[4] 

... an array of 4 ...

 *a[4] 

... pointers to ...

 (*a[4])() 

... function without parameters ...

 *(*a[4])() 

... returning a pointer to ...

 (*(*a[4])())[5] 

... an array of five ...

 char (*(*a[4])())[5] 

... characters.

Sidenote: Go on, give the architect who invented this good dressing, then find the programmer who wrote this code, without a comment explaining it and giving him good dressing. In case this was given to you as homework, tell your teacher that he should instruct you on how to use cdecl instead, or how to design the code so that it does not look like crazy scribbles, instead of to spend your time with this.

+25
source

And one more example ... of something that should never be done in anything but an example.

 #include <iostream> typedef char stuff[5]; stuff stuffarray[4] = { "This", "Is", "Bad", "Code" }; stuff* funcThis() { return &(stuffarray[0]); } stuff* funcIs() { return &(stuffarray[1]); } stuff* funcBad() { return &(stuffarray[2]); } stuff* funcCode() { return &(stuffarray[3]); } int main() { char (*(*a[4])())[5] = { funcThis, funcIs, funcBad, funcCode }; for(int i = 0; i < 4; ++i) { std::cout << *a[i]() << std::endl; } return 0; } 
+13
source

And here is an example ...

 #include <stdio.h> char a[5] = "abcd"; char b[5] = "bcde"; char c[5] = "cdef"; char d[5] = "defg"; char (*f1())[5] { return &a; } char (*f2())[5] { return &b; } char (*f3())[5] { return &c; } char (*f4())[5] { return &d; } int main() { char (*(*a[4])())[5] = { &f1, &f2, &f3, &f4 }; for (int i = 0; i < 4; i++) printf("%s\n", *a[i]()); return 0; } 
+8
source

All Articles