Declaration of pointers; an asterisk to the left or right of the space between the type and the name?

Possible duplicates:
What makes sense - char * or char * string? C ++ pointer declarations: star placement

I have seen mixed versions of this code. (By the way, this applies to C and C ++.) People seem to declare pointers in one of two ways, and I don't know which of them is right, even if it matters.

The first way to bind an asterisk to a type name, for example:

someType* somePtr; 

The second way is to put an asterisk next to the variable name, for example:

 someType *somePtr; 

This has been driving me crazy for several years. Is there a standard way to declare pointers? It doesn't matter how pointers are declared? I used to use both declarations, and I know that the compiler doesn't care how this happens. However, the fact that I saw pointers declared in two different ways makes me believe that there is a reason. I am curious if any method is more readable or logical in some way that I am missing.

+76
c ++ c pointers
Apr 18 2018-10-18T00:
source share
6 answers

This is a matter of preference and somewhat holy war, as is the brace style.

Style

 someType* somePtr; 

emphasizes the type of pointer variable. He says essentially " somePtr type is a pointer to someType ."

Style

 someType *somePtr 

emphasizes the type of pointy data. He says, essentially, "the data type that somePtr , someType ."

Both of them mean the same thing, but it depends on whether the given mental model of the programmer when creating the pointer is "so", so to speak, according to pointed data or a pointer variable.

Put it in the middle (like someType * somePtr ) to avoid fixing one of them.

+67
Apr 18 2018-10-18T00:
source share

It does not matter. Someone will come now and close the question as a hoax, and someone else will show how the int* a break breaks if you declare several variables in the same declarations, while the int *a way better reflects the syntax structure of the code and the other the guy will show that Straustup prefers the path int* a .

Many opinions, but there is no "right" way.

+78
Apr 18 2018-10-18T00:
source share

It does not matter, it is a 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 simply miss this and agree with both options.

+42
Apr 18 2018-10-18T00:
source share

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

 int *pValue; 

reads "dereference pValue is int" whereas a C ++ programmer thinks in "types", therefore

 int* pValue; 

reads "type pValue is a pointer to int" The compiler does not see the difference at all. However, you will find that the C programmer insists on “semantics of values” when programming in C ++.

+18
Apr 18 2018-10-18T00:
source share

I think adding an asterisk next to the variable name is clearer.

You may 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 someType *one, *two avoids this problem.

+13
Apr 18 '10 at 0:44
source share

Every time I ever saw

 TheType *myPointer 

because you declare a POINTER of type TheType. Similar announcement

 TheType myVar 

will declare an instance variable of type TheType.

You can also do this clearly and read easily.

 TheType myVar, *myPointer; 
-four
Apr 18 2018-10-18T00:
source share



All Articles