where to place * in pointer type

Pages: 1234
Could anyone please let me know what is the right or standard way to place * in pointer types?

int * p;
int *p;
int* p;



from above three what is the right/standard way ?

Even in this website documentation about pointer it has used mixed format from above but not clearly mentioned what is the right way .

Thank you in advance.
There is no single right way. Both int* p; and int *p; are regularly used.

int * p; is a lot less common and, at least to me, looks weird and a bit like multiplication.

But I do use the extra space when declaring "const const" strings like char const * const s; and similar.

Whichever you choose, you should be consistent!

For another (!) opinion, see:

Is ``int* p;'' right or is ``int *p;'' right?

http://www2.research.att.com/~bs/bs_faq2.html#whitespace

Andy
Last edited on
For better readable code you should use int *p; because of the alignment of the pointertype.
For example:
int* var1, var2;
Here var1 is a pointer and var2 is normal integer. I prefer the int* p1; notation, too. But you should everytime remind the alignment...
Well, int *p; versus is int* p; is very much a matter of taste. I personally find int* p; reads better as I see the int* as the type and p as the variable name.

But then I would never code int* var1, var2; as it breaks the one-declaration-per-line and initialize-all-varaible rules. I would code this as

1
2
int* var1 = NULL;
int* var2 = NULL;


And in general, as a C++ programmer, I use declaration-at-point-of-first-use. That is,

1
2
3
4
5
// other stuff

const char* name = obg.GetName();

// etc 


rather than

1
2
3
4
5
6
7
const char* name = NULL;

// other stuff

name = obg.GetName();

// etc 


So there's not stacks of variables to declare (one row at a time) or initialize at the start of a function in the first place.

Last edited on
its all a matter of style.

for example imo it is better to type

1
2
foo* i = new foo();
//type-attribute-name-initalization 


because it seems more natural to myself,(continuous motion), than

1
2
3
4
5
6
foo[space]*[space]i = new foo();

or 

foo[space]*i = new foo();


now apply the same concept to 'int' or any other type.
Last edited on
int *p makes more sense. It makes it clear that *p is an int. int* p makes it seem like there is actually a pointer-to-int type, which doesn't exist.

And putting it in the middle is just weird.
Last edited on
All three declarations are equivalent. However in my opinion it is better to specidy

int *p;

Why? Because very often programmers make the false assumpltion that the following declaration

int* p1, p2;

declares two pointers. However in realty only p1 is a pointer to int while p2 has type int.
So then your declare as

int *p1, p2;

you will not think that p1 and p2 are both pointers.
Last edited on
ascii wrote:
int* p makes it seem like there is actually a pointer-to-int type, which doesn't exist.
If p isn't of type "pointer-to-int" what type does it have? To me it makes very much sense because I like to think that p is a pointer to an int.
Last edited on
foo* i = new foo(); is likely to be a misuse
^depends on the programmer who uses it.
i read int* p as int_pointer p
as in a pointer named p to a data segment of int size.

edit: funny how i read backwards
Last edited on
If p isn't of type "pointer-to-int" what type does it have? To me it makes very much sense because I like to think that p is a pointer to an int.

Well you can think of it that way, what I'm trying to say is that writing it like int* p implies that there is actually a int* type. This does not exist (think about the type of x in int* p, x;). Saying int *p is clearly defining *p as an int. p doesn't really have a primitive type.
As far as I am concerned, if I write char* p; then the type of p is a char* (or pointer to int). Not everyone buys into this interpretation, but I'm not alone.

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.

(Stroustup, http://www2.research.att.com/~bs/bs_faq2.html#whitespace)

Of course, there is no primitive (or built in) type int*. But if I follow the same logic, that only primitive type are actually types, structs and classes (being compound) cannot actually be types, either. This I obviously do not buy into!
Last edited on
Well, I disagree :0
Stroustrup is very weak as a practical programmer.:)
I am aware that some people have a non-type-centric view (Stroustrup sees them as being focused on syntax rather than type).

But others do.

I just wanted to emphasise that while some people do not see char* as a type, others do (including me and Stroustrup).

And that the statement that char* -- as a type -- does not exist is not a statement of fact. It's just some peoples interpretation.

As is my interpretation that it is a type!

e.g. a function with a return type of const char*, with its result being assigned to a variable of the same type.

const char* example = getExample();
Last edited on
@andywestken

And that the statement that char* -- as a type -- does not exist is not a statement of fact. It's just some peoples interpretation.

As is my interpretation that it is a type!


Well, then according to your interpretation of the declaration

const char* p1, s;

it follows that s has type const char*.

* is not a type specifier (it is not an interpretation it is a statement of the C/C++ standard). * is used in a declarator. So it is a good style of programming to separate type specifiers and declarators. For a given type specifier many declarators can be specified in a declaration specifier. So it is more consistent to write

const char *p, s;


where *p and s are declarators and const char is a type specifier
than

const char* p, s;

In fact in the example above you break the grammar of the C++ language.

I can demonstrate you a more interesting example that makes you style senseless. Consider the following declaration


int* p, f( int, int, int );

Now answer what is return type of function f?:)
Last edited on
+1 vote for the type-centric int* p;.

Declaring multiple objects of different type category on the same line is silly: if you like writing

int n, *p, (&fref)(void) = f, a[10];

then it's up to you to remember what happens.
Last edited on
@Cubbi

it's up to you to remember what happens


It says only about your bad style of programming because instead of to help reviewers to understand easy all syntactic constructions you say them "it's up to you to remember what happens".

Weak programmers think that only they read their code. More experienced programmers as you think that their code can be read by only C/C++ programmers and "it's up to you to remember what happens". And only profeesional programmers understand that their code can be read by others who does not know C++. And the task of professional programmers is to help these others to understand their code and do not say "it's up to you to remember what happens".
Last edited on
instead of to help reviewers to understand

To help the reviewers understand, most C++ coding styles limit the number of object declarations to one per line. Type-centric approach is much more important than compressing the source code.
Last edited on
Pages: 1234