Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does c++ pointer * associate to the variable declared, not the type?

Tags:

Why was C++ designed such that the correct way to declare two int *s on the same line is

int *x, *y; 

not

int* x,y; 

I know some people think you should avoid either form and declare every variable on its own line, but I'm interested in why this language decision was made.

like image 887
Sideshow Bob Avatar asked Jun 14 '12 12:06

Sideshow Bob


People also ask

What does * indicate in pointer in C?

A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.

What does * mean before a variable in C?

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value. In the C programming language, the deference operator is denoted with an asterisk (*).

Why do we declare pointer with *?

int variable1; int variable2; char variable3; int *addressOfVariables; * – A pointer variable is a special variable in the sense that it is used to store an address of another variable. To differentiate it from other variables that do not store an address, we use * as a symbol in the declaration.

Can pointer refer to any type?

A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer to a bit field or a reference.


2 Answers

To keep compatibility with C code, because that's how C works.

Bjarne makes a good point in his style and technique faq:

The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

So, the motivation for this working as this in C++ is how it works in C.

The motivation it works like that in C is that, as stated above, C emphasizes expressions rather than types.

like image 160
Luchian Grigore Avatar answered Sep 18 '22 18:09

Luchian Grigore


The simple answer is: because that's the way C does it. Which, of course, only begs the question: why does C do it this way?

The original philosophy, in early C, is that the declaration be an exact image of the use. So when you write:

int *p; 

, you are declaring that the expression *p has type int (and the compiler works out the actual type of p accordingly).

This, of course, ceased to be true the day C introduced typedef, and later struct. And any resemblance disappeared completely with const (first introduced in C++, then retrofitted into C), where things like

int *const p; 

have no relationship with use. But by then, the die was cast.

like image 32
James Kanze Avatar answered Sep 17 '22 18:09

James Kanze