Placement of the asterisk in pointer declarations

前端 未结 13 869
闹比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:23

    In my opinion, the answer is BOTH, depending on the situation. Generally, IMO, it is better to put the asterisk next to the pointer name, rather than the type. Compare e.g.:

    int *pointer1, *pointer2; // Fully consistent, two pointers
    int* pointer1, pointer2;  // Inconsistent -- because only the first one is a pointer, the second one is an int variable
    // The second case is unexpected, and thus prone to errors
    

    Why is the second case inconsistent? Because e.g. int x,y; declares two variables of the same type but the type is mentioned only once in the declaration. This creates a precedent and expected behavior. And int* pointer1, pointer2; is inconsistent with that because it declares pointer1 as a pointer, but pointer2 is an integer variable. Clearly prone to errors and, thus, should be avoided (by putting the asterisk next to the pointer name, rather than the type).

    However, there are some exceptions where you might not be able to put the asterisk next to an object name (and where it matters where you put it) without getting undesired outcome — for example:

    MyClass *volatile MyObjName

    void test (const char *const p) // const value pointed to by a const pointer

    Finally, in some cases, it might be arguably clearer to put the asterisk next to the type name, e.g.:

    void* ClassName::getItemPtr () {return &item;} // Clear at first sight

    0 讨论(0)
  • 2020-11-22 05:24

    4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:

    int *test, *test2;
    

    Or, even better (to make everything clear):

    int* test;
    int* test2;
    
    0 讨论(0)
  • 2020-11-22 05:27

    The pointer is a modifier to the type. It's best to read them right to left in order to better understand how the asterisk modifies the type. 'int *' can be read as "pointer to int'. In multiple declarations you must specify that each variable is a pointer or it will be created as a standard variable.

    1,2 and 3) Test is of type (int *). Whitespace doesn't matter.

    4,5 and 6) Test is of type (int *). Test2 is of type int. Again whitespace is inconsequential.

    0 讨论(0)
  • 2020-11-22 05:32

    White space around asterisks have no significance. All three mean the same thing:

    int* test;
    int *test;
    int * test;
    

    The "int *var1, var2" is an evil syntax that is just meant to confuse people and should be avoided. It expands to:

    int *var1;
    int var2;
    
    0 讨论(0)
  • 2020-11-22 05:35

    As others mentioned, 4, 5, and 6 are the same. Often, people use these examples to make the argument that the * belongs with the variable instead of the type. While it's an issue of style, there is some debate as to whether you should think of and write it this way:

    int* x; // "x is a pointer to int"
    

    or this way:

    int *x; // "*x is an int"
    

    FWIW I'm in the first camp, but the reason others make the argument for the second form is that it (mostly) solves this particular problem:

    int* x,y; // "x is a pointer to int, y is an int"
    

    which is potentially misleading; instead you would write either

    int *x,y; // it's a little clearer what is going on here
    

    or if you really want two pointers,

    int *x, *y; // two pointers
    

    Personally, I say keep it to one variable per line, then it doesn't matter which style you prefer.

    0 讨论(0)
  • 2020-11-22 05:36

    The rationale in C is that you declare the variables the way you use them. For example

    char *a[100];
    

    says that *a[42] will be a char. And a[42] a char pointer. And thus a is an array of char pointers.

    This because the original compiler writers wanted to use the same parser for expressions and declarations. (Not a very sensible reason for a langage design choice)

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