Complex declarations

后端 未结 8 1470
伪装坚强ぢ
伪装坚强ぢ 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:50

    Here is a great article about how to read complex declarations in C: http://www.codeproject.com/KB/cpp/complex_declarations.aspx

    It helped me a lot!

    Especially - You should read "The right rule" section. Here quote:

    int * (* (*fp1) (int) ) [10]; This can be interpreted as follows:

    1. Start from the variable name -------------------------- fp1
    2. Nothing to right but ) so go left to find * -------------- is a pointer
    3. Jump out of parentheses and encounter (int) --------- to a function that takes an int as argument
    4. Go left, find * ---------------------------------------- and returns a pointer
    5. Jump put of parentheses, go right and hit [10] -------- to an array of 10
    6. Go left find * ----------------------------------------- pointers to
    7. Go left again, find int -------------------------------- ints.

提交回复
热议问题