Placement of the asterisk in pointer declarations

前端 未结 13 870
闹比i
闹比i 2020-11-22 04:36

I\'ve recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition.

相关标签:
13条回答
  • 2020-11-22 05:40

    Many coding guidelines recommend that you only declare one variable per line. This avoids any confusion of the sort you had before asking this question. Most C++ programmers I've worked with seem to stick to this.


    A bit of an aside I know, but something I found useful is to read declarations backwards.

    int* test;   // test is a pointer to an int
    

    This starts to work very well, especially when you start declaring const pointers and it gets tricky to know whether it's the pointer that's const, or whether its the thing the pointer is pointing at that is const.

    int* const test; // test is a const pointer to an int
    
    int const * test; // test is a pointer to a const int ... but many people write this as  
    const int * test; // test is a pointer to an int that's const
    
    0 讨论(0)
提交回复
热议问题