How do you decipher complex declarations of pointers+arrays?

后端 未结 7 1584
醉话见心
醉话见心 2021-02-06 17:08

Although I use std::vector almost all the time, I am interested in understanding as much as I can about pointers. Examples of what I am talking about:



        
相关标签:
7条回答
  • 2021-02-06 17:40

    you always read pointers from right to left interpreting the '*' as a pointer. for example char** a[5] is an array of 5 pointers to pointers of characters...

    0 讨论(0)
  • 2021-02-06 17:46

    The general procedure for reading a type in C/C++ is:

    1. Identify the final type, which can be a basic type or a typedef identifier, and which can have type modifiers such as const, volatile, etc. In your example that's "char".
    2. Apply the operators to the identifier in the same order of precedence as they have in expressions. These operators can be * (dereference), [] (index) and () (call function).

    In the original philosophy of the syntax, your example would have been written "char *array[5]", the identifier is "array" and the operators are [] (index) then * (dereference).

    The declaration then reads like a contract "if you apply these operators in that order, then you get an object of the final type".

    In your case the full sentence is "if you index the variable "array", then dereference the resulting expression, you get a char".

    You can also consider it like that "if you index the variable "array", then you get an object such that if you dereference it, you get a char"

    The trick is mostly to keep track of the fact that [] and () have a higher precedence than *. You can control the operator order with parentheses just like for regular expressions.

    0 讨论(0)
  • 2021-02-06 17:48

    Not just pointers and arrays: How to interpret complex C/C++ declarations:

    Start reading the declaration from the innermost parentheses, go right, and then go left. When you encounter parentheses, the direction should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole declaration has been parsed.

    One small change to the right-left rule: When you start reading the declaration for the first time, you have to start from the identifier, and not the innermost parentheses.

    You example:

    char* array[5];
    

    Is an array of 5 pointers to char.

    0 讨论(0)
  • 2021-02-06 17:51

    cdecl is a program which is nice for this sort of thing. (particularly when you add function pointers into the mix!)

    Type `help' or `?' for help
    cdecl> explain char* foo[5]
    declare foo as array 5 of pointer to char
    cdecl> declare bar as array 5 of pointer to function (integer, integer) returning char
    char (*bar[5])(int , int )
    
    0 讨论(0)
  • 2021-02-06 17:58

    I learned the clockwise/spiral rule long ago from some magazine article. Here's an online article that describes the technique:

    • http://c-faq.com/decl/spiral.anderson.html

    It's served me well, though I still struggle mightily with some of the monstrous template-based declarations I come across at times.

    0 讨论(0)
  • 2021-02-06 17:58

    char * is the type and you have an array of 5 of them.

    0 讨论(0)
提交回复
热议问题