int* vs int *

M

Mister X

Hi

Could someone explain the difference between:

int *x;
int* x;

how, why, history, etc.
 
S

saurabh

Hi

Could someone explain the difference between:

int    *x;
int*   x;

how, why, history, etc.

it doesn't matter and is a matter of choice/style as long as you are
declaring a sigle variable.
But when you want to declare more than one such pointer,it is no
longer a matter of style.
e.g.

int* x,y;//line 1;
int *x,*y;//line 2; notice the * in both of thsese

In line 1, x is a pointer to an int while y is an ordinary int.
In line 2 x and y both are pointer to int.

Saurabh
 
B

bartc

Mister X said:
Hi

Could someone explain the difference between:

int *x;
int* x;

These are interchangeable. The star however belongs to the following name,
not the type, so the second form would look misleading (if there were more
names following).

But in a cast:

p = (int*) a;

The star in this case belongs to the type.
 
B

Ben Bacarisse

bartc said:
These are interchangeable. The star however belongs to the following
name, not the type, so the second form would look misleading (if there
were more names following).
Agreed.

But in a cast:

p = (int*) a;

The star in this case belongs to the type.

I disagree. I think the best way to think of the type in a cast is as
a declaration with the name "" (or with the name removed if you
prefer) so the * belongs snuggled up to the missing name:

p = (int *)a;

This is most obvious with pointer-to-array and a pointer-to-function
types:

(int (*)[3])
(int (*)(int, int, int))

Here, the * is still a "top-level one" -- the cast is to a pointer
type -- but it is not int to which the * "belongs". It does not even
belong to "[3]" or "(int, int, int)", but to the missing name that
would have been in the brackets with it, had this been a declaration
rather than a type name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,105
Latest member
sheetaldubay7750ync
Top