How do you decipher complex declarations of pointers+arrays?

后端 未结 7 1593
醉话见心
醉话见心 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: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.

提交回复
热议问题