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

后端 未结 8 1236
终归单人心
终归单人心 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: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.

提交回复
热议问题