In C, what is the correct syntax for declaring pointers?

后端 未结 8 1213
终归单人心
终归单人心 2020-11-27 20:32

I vaguely recall seeing this before in an answer to another question, but searching has failed to yield the answer.

I can\'t recall what is the proper way t

相关标签:
8条回答
  • 2020-11-27 20:35

    It's an accident of C syntax that you can write it either way; however, it is always parsed as

    Type (*pointer);
    

    that is, the * is always bound to the declarator, not the type specifier. If you wrote

    Type* pointer1, pointer2;
    

    it would be parsed as

    Type (*pointer1), pointer2;
    

    so only pointer1 would be declared as a pointer type.

    C follows a "declaration mimics use" paradigm; the structure of a declaration should mimic the equivalent expression used in the code as much as possible. For example, if you have an array of pointers to int named arr, and you want to retrieve the integer value pointed to by the i'th element in the array, you would subscript the array and dereference the result, which is written as *arr[i] (parsed as *(arr[i])). The type of the expression *arr[i] is int, so the declaration for arr is written as

    int *arr[N];
    

    Which way is right? Depends on who you ask. Old C farts like me favor T *p because it reflects what's actually happening in the grammar. Many C++ programmers favor T* p because it emphasizes the type of p, which feels more natural in many circumstances (having written my own container types, I can see the point, although it still feels wrong to me).

    If you want to declare multiple pointers, you can either declare them all explicitly, such as:

    T *p1, *p2, *p3;
    

    or you can create a typedef:

    typedef T *tptr;
    tptr p1, p2, p3;
    

    although I personally don't recommend it; hiding the pointer-ness of something behind a typedef can bite you if you aren't careful.

    0 讨论(0)
  • 2020-11-27 20:42

    Those two are the same. However, if you do multiple declarations, the former can trap you.

    int* pi, j;

    declares an int pointer (pi) and an int (j).

    0 讨论(0)
  • 2020-11-27 20:43

    All of these will compile, are legal and are equivalent:

    Type* instance;
    Type * instance;
    Type *instance;
    

    Pick a style and be consistent.

    0 讨论(0)
  • 2020-11-27 20:46

    Those two are exactly the same.

    However, for the purposes of declaring multiple variables, as in below:

    int * p1, p2;
    

    where p1 is of type pointer to int, but p2 is of type int. If you wished to declare p2 as a pointer to int, you have to write:

    int *p1, *p2;
    
    0 讨论(0)
  • 2020-11-27 20:49

    Curiously, this rule doesn't apply if the type is deduced in C++0x.

    int a;
    decltype(&a) i, j;
    

    Both i and j are int*.

    0 讨论(0)
  • 2020-11-27 20:52

    It is simply a matter of how you like to read it.

    The reason that some people put it like this:

    Type *instance;
    

    Is because it says that only instance is a pointer. Because if you have a list of variables:

    int* a, b, c;
    

    Only a is a pointer, so it's easier like so

    int *a, b, c, *d;
    

    Where both a and d are pointers. It actually makes no difference, it's just about readability.

    Other people like having the * next to the type, because (among other reasons) they consider it a "pointer to an integer" and think the * belongs with the type, not the variable.

    Personally, I always do

    Type *instance;
    

    But it really is up to you, and your company/school code style guidelines.

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