tags:

views:

198

answers:

7

Possible Duplicates:
What makes more sense - char* string or char *string?
What’s your preferred pointer declaration style, and why?

I've seen mixed versions of this in a lot of code. (This applies to C and C++, by the way.) People seem to declare pointers in one of two ways, and I have no idea which one is correct, of if it even matters.

The first way it to put the asterisk adjacent the type name, like so:

someType* somePtr;

The second way is to put the asterisk adjacent the name of the variable, like so:

someType *somePtr;

This has been driving me nuts for some time now. Is there any standard way of declaring pointers? Does it even matter how pointers are declared? I've used both declarations before, and I know that the compiler doesn't care which way it is. However, the fact that I've seen pointers declared in two different ways leads me to believe that there's a reason behind it. I'm curious if either method is more readable or logical in some way that I'm missing.

EDIT: I asked this because for the longest time, I wrote code using the method with the asterisk on the left, next to the type. (This is how I learned to declare pointers.) One day, I came upon a bit of code with multiple alike pointers, and I tried to declare them on the same line. I soon figured out that multiple pointer declarations on the same line don't work well with the way I learned to declare them, and I've been trying to figure out how to remain consistent ever since.

EDIT: My apologies for repeating a common question. I figured it must have been asked before, but it's not an easy question to search for. Thank you for your answers and clarification.

+6  A: 

It doesn't matter, it is personal preference.

Some people like to keep the type together:

int* p;

Other people say that it should go next to the variable because of the following:

int *p, x;//declare 1 int pointer and 1 int
int *p, *x;//declare 2 int pointers.

Over time you will just overlook this and accept both variations.

Brian R. Bondy
Hopefully, I never liked asterisks.
jweyrich
A: 

Every single way I've seen ever is

TheType *myPointer

because you are declaring a POINTER of type TheType. Similar declaring

TheType myVar

would be declaring an instance variable of type TheType.

Also you can then clearly do this and have it easily readable

TheType myVar, *myPointer;

That's my 2 cents

Driss Zouak
+2  A: 

It's a matter of preference, and somewhat of a holy war, just like brace style.

The style

someType* somePtr;

is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr is pointer-to-someType".

The style

someType *somePtr

is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr is someType".

They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.

Putting it in the middle (as someType * somePtr) is trying to avoid committing to either one.

Tyler McHenry
+1  A: 

It doesn't matter. Someone will now come along and close the question as a dupe, and someone else will show how the int* a way breaks if you declare multiple variables in the same declarations while the int *a way better reflects the syntactical structure of the code, and another guy shows that stroustrup prefers the int* a way.

Much opinions, but no "right" way here.

Johannes Schaub - litb
A true master of SO to predict all of this early :)
Brian R. Bondy
A: 

There is absolutely no difference between the two methods, it's more like the you like it most. I use the someType* somePtr way, because the asterix changes the type, it's not a someType object now, it's a someType pointer, so I prefer this way because I think it's easier to read.

If you work for some company as a programmer, you will probably be given a "Programming Standard" list, but it's different from an employer to another.

A: 

I think putting the asterisk adjacent to the name of the variable is clearer.

You might mistakenly declare someType* one, two; thinking that they are both pointers but only the variable one is a pointer; two is just a someType. Declaring as someType *one, *two avoids this problem.

suihock
+1  A: 

The difference arose because C++ added a stronger type system on top of C. A C programmer usually thinks in terms of "values," so

int  *pValue;

reads "the dereference of pValue is an int" whereas a C++ programmer thinks in "types" so

int* pValue;

reads "the type pValue is pointer to int" The compiler sees no difference at all of course. However you will find that it is the C programmer who insist on "value semantics" when programming in C++.

Lance Diduck