Pointer syntax in C: why does * only apply to the first variable?

后端 未结 7 575
南方客
南方客 2020-12-09 16:30

The following declaration in C:

int* a, b;

will declare a as type int* and b as type int

7条回答
  •  攒了一身酷
    2020-12-09 16:47

    Because if the statement

    int* a, b;
    

    were to declare b as a pointer too, then you would have no way to declare

    int* a;
    int  b;
    

    on a single line.

    On the other hand, you can do

    int*a, *b;
    

    to get what you want.

    Think about it like that: the way it is now it is still the most concise and yet unique way to do it. That's what C is mostly about :)

提交回复
热议问题