Why is the asterisk before the variable name, rather than after the type?

后端 未结 12 1660
时光取名叫无心
时光取名叫无心 2020-11-22 08:53

Why do most C programmers name variables like this:

int *myVariable;

rather than like this:

int* myVariable;
相关标签:
12条回答
  • 2020-11-22 09:22

    Because the * in that line binds more closely to the variable than to the type:

    int* varA, varB; // This is misleading
    

    As @Lundin points out below, const adds even more subtleties to think about. You can entirely sidestep this by declaring one variable per line, which is never ambiguous:

    int* varA;
    int varB;
    

    The balance between clear code and concise code is hard to strike — a dozen redundant lines of int a; isn't good either. Still, I default to one declaration per line and worry about combining code later.

    0 讨论(0)
  • 2020-11-22 09:23

    For declaring multiple pointers in one line, I prefer int* a, * b; which more intuitively declares "a" as a pointer to an integer, and doesn't mix styles when likewise declaring "b." Like someone said, I wouldn't declare two different types in the same statement anyway.

    0 讨论(0)
  • 2020-11-22 09:25

    If you look at it another way, *myVariable is of type int, which makes some sense.

    0 讨论(0)
  • 2020-11-22 09:25

    That's just a matter of preference.

    When you read the code, distinguishing between variables and pointers is easier in the second case, but it may lead to confusion when you are putting both variables and pointers of a common type in a single line (which itself is often discouraged by project guidelines, because decreases readability).

    I prefer to declare pointers with their corresponding sign next to type name, e.g.

    int* pMyPointer;
    
    0 讨论(0)
  • 2020-11-22 09:27

    A great guru once said "Read it the way of the compiler, you must."

    http://www.drdobbs.com/conversationsa-midsummer-nights-madness/184403835

    Granted this was on the topic of const placement, but the same rule applies here.

    The compiler reads it as:

    int (*a);
    

    not as:

    (int*) a;
    

    If you get into the habit of placing the star next to the variable, it will make your declarations easier to read. It also avoids eyesores such as:

    int* a[10];
    

    -- Edit --

    To explain exactly what I mean when I say it's parsed as int (*a), that means that * binds more tightly to a than it does to int, in very much the manner that in the expression 4 + 3 * 7 3 binds more tightly to 7 than it does to 4 due to the higher precedence of *.

    With apologies for the ascii art, a synopsis of the A.S.T. for parsing int *a looks roughly like this:

          Declaration
          /         \
         /           \
    Declaration-      Init-
    Secifiers       Declarator-
        |             List
        |               |
        |              ...
      "int"             |
                    Declarator
                    /       \
                   /        ...
               Pointer        \
                  |        Identifier
                  |            |
                 "*"           |
                              "a"
    

    As is clearly shown, * binds more tightly to a since their common ancestor is Declarator, while you need to go all the way up the tree to Declaration to find a common ancestor that involves the int.

    0 讨论(0)
  • 2020-11-22 09:27

    Because it makes more sense when you have declarations like:

    int *a, *b;
    
    0 讨论(0)
提交回复
热议问题