Complex declarations

后端 未结 8 1467
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 05:03

How do I interpret complex declarations like:

int * (* (*fp1) (int) ) [10]; ---> declaration 1
int *( *( *[5])())(); --------> declaration 2

8条回答
  •  隐瞒了意图╮
    2020-11-30 05:44

    I've learned the following method long ago:

    Start from the type identifier (or the inner parenthesis) and move following a spiral taking the element at right first

    In case of

     int * (* (*fp1) (int) ) [10];
    

    You can say:

    • fp1 is a (nothing on the right so move left)
    • pointer to (move out of the inner parenthesis
    • a function taking int as agument (the 1st on the right)
    • and returns a pointer to (exit from parenthesis)
    • an array of 10 elements of type
    • pointer to (nothing left on the right)
    • int

    Resulting in:

    fp1 is a pointer to a function taking an int and returning a pointer to an array of 10 pointers to int

    Drawing the actual spiral (in you your mind, at least) helps a lot.

提交回复
热议问题