NlightNFotis
NlightNFotis

Reputation: 9805

Semantic difference between different pointer syntax in c++?

In C++, is this form int *p semantically different than this one int* p? Or is this clearly a matter of style?

Now this question may be dumb, but I remember that I had seen somewhere both styles being used at the same time, something that led to me to believe that there may be different semantics between those forms.

Upvotes: 3

Views: 223

Answers (3)

j_random_hacker
j_random_hacker

Reputation: 51226

As others have said, they're the same.

The fact that int* a, b; declares one pointer and one integer is one aspect of a wart that manifests in other ways too. E.g.:

int* a, f(double), c[42];

declares a pointer to int called a, a function that takes a double and returns an int called f, and an array of 42 ints called c. That's because all of these "decorations" (*, (), []) form part of the C++ grammar called the declarator, which is associated with an individual name, rather than with the statement as a whole (which is called a declaration in the grammar).

Upvotes: 2

Robertas
Robertas

Reputation: 1222

It has the same semantic meaning. If you are using const keyword be aware of the caveats:

int * conts p; - const pointer to int versus

int const * p; - non const pointer to const int.

Upvotes: 4

Kos
Kos

Reputation: 72241

They're the same.

That's tricky sometimes, since

int* a, b;

defines one pointer and one int. This style makes it more clear:

int *a, b;

Upvotes: 6

Related Questions