0

During a C introductory course (in an Engineering University), we were asked to identify a declaration with pointers along the lines of int (*(*f[5])(void))[10];.

my current understanding of the declaration would be "an array containing 10 function pointers returning an int pointer each and not taking any args".

Could someone confirm my understanding of the declaration, and tell me if such definitions would be of any use in practice ?

jzaehrin
  • 9
  • 2
  • 2
    I suggest you follow [the clockwise spiral rule](http://c-faq.com/decl/spiral.anderson.html). Starting from the variable (`f` in your case) and go outwards. – Some programmer dude May 04 '18 at 09:59
  • 1
    https://cdecl.org/ might also be helpful. – Some programmer dude May 04 '18 at 10:02
  • 3
    In practice, not many C programmers would know what this declaration means. If they needed something complicated like this, they'd use typedefs to make it more readable. – interjay May 04 '18 at 10:09
  • 2
    Possible duplicate of [C isn't that hard: void ( \*( \*f\[\] ) () ) ()](https://stackoverflow.com/questions/34548762/c-isnt-that-hard-void-f) – msc May 04 '18 at 10:11
  • This is a typical school assignment to learn how C declarations work. And when you have learned that, you have also learned never to do this. Except possibly for a code review on April 1. – Bo Persson May 04 '18 at 10:48

1 Answers1

5

int (*(*f[5])(void))[10] declares (*(*f[5])(void))[10] to be an int.

Which means (*(*f[5])(void)) is an array of 10 int.

Which means (*f[5])(void) is a pointer to an array of 10 int.

Which means (*f[5]) is a function taking void and returning a pointer to an array of 10 int.

Which means f[5] is a pointer to a function taking void and returning a pointer to an array of 10 int.

Which means f is an array of 5 pointers to functions taking void and returning a pointer to an array of 10 int.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312