Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

The following declaration in C:

int* a, b; 

will declare a as type int* and b as type int. I'm well aware of this trap, but what I want to know is why it works this way. Why doesn't it also declare b as int*, as most people would intuitively expect? In other words, why does * apply to the variable name, rather than the type?

Sure you could write it this way to be more consistent with how it actually works:

int *a, b; 

However, I and everyone I've spoken to think in terms of a is of type "pointer to int", rather than a is a pointer to some data and the type of that data is "int".

Was this simply a bad decision by the designers of C or is there some good reason why it's parsed this way? I'm sure the question has been answered before, but I can't seem to find it using the search.

like image 998
EMP Avatar asked Jul 15 '10 23:07

EMP


People also ask

What does * and & indicate in pointer in C?

& symbol is used to get the address of the variable. * symbol is used to get the value of the variable that the pointer is pointing to. If a pointer in C is assigned to NULL, it means it is pointing to nothing. Two pointers can be subtracted to know how many elements are available between these two pointers.

Which is the correct way to declare a pointer *?

The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too. Naming Convention of Pointers: Include a " p " or " ptr " as prefix or suffix, e.g., iPtr , numberPtr , pNumber , pStudent .

What is the primary purpose of pointer variables?

Pointers are used to pass parameters by reference. This is useful if the programmer wants a function's modifications to a parameter to be visible to the function's caller. This is also useful for returning multiple values from a function.

What is pointer variable in C?

A pointer is a variable that stores the memory address of another variable as its value. A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.


1 Answers

C declarations were written this way so that "declaration mirrors use". This is why you declare arrays like this:

int a[10]; 

Were you to instead have the rule you propose, where it is always

type identifier, identifier, identifier, ... ; 

...then arrays would logically have to be declared like this:

int[10] a; 

which is fine, but doesn't mirror how you use a. Note that this holds for functions, too - we declare functions like this:

void foo(int a, char *b); 

rather than

void(int a, char* b) foo; 

In general, the "declaration mirrors use" rule means that you only have to remember one set of associativity rules, which apply to both operators like *, [] and () when you're using the value, and the corresponding tokens in declarators like *, [] and ().


After some further thought, I think it's also worth pointing out that spelling "pointer to int" as "int*" is only a consequence of "declaration mirrors use" anyway. If you were going to use another style of declaration, it would probably make more sense to spell "pointer to int" as "&int", or something completely different like "@int".

like image 138
caf Avatar answered Nov 24 '22 05:11

caf