Poll: How do you declare c/c++/c# style pointers?

What syntax do you use?

  • int* pointer

  • int * pointer

  • int *pointer

  • More than one


Results are only viewable after voting.

irishScott

Lifer
Oct 10, 2006
21,562
3
0
And what's your reasoning? Just curious. Everywhere I go I see people using "int *p;" or occasionally "int * p", I seem to be the only one who uses "int* p". It makes sense to me because the type of variable in question is an "int pointer". The asterisk is essentially part of the type-name. Can read the other variations fine of course, but when I do it just mildly spreads out the reading comprehension.

In my mind:
int* sam = "int pointer sam"
int * sam = "int.. pointer sam
int *sam = "int pointersam"
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
I wish the asterisk was part of the typename, but it's not. So we get this problem:
int* p, p2; // oops, p2 is int
which would be easier to spot and avoid if you always used "int *x":
int *p, *p2;
Anyway, I occasionally use both "int* foo" and "int *foo", but not "int * foo". That just looks stupid somehow.
I think generally the best is to use "int* foo" and just not declare multiple pointers at the same time.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Prefer:
int *p;

Doing so, removes any potential chance of what others showed (confusion)
int *p, p1;

Also, I prefer to not mix and match
int *p;
int p1;
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
And what's your reasoning? Just curious. Everywhere I go I see people using "int *p;" or occasionally "int * p", I seem to be the only one who uses "int* p". It makes sense to me because the type of variable in question is an "int pointer". The asterisk is essentially part of the type-name. Can read the other variations fine of course, but when I do it just mildly spreads out the reading comprehension.

In my mind:
int* sam = "int pointer sam"
int * sam = "int.. pointer sam
int *sam = "int pointersam"

I personally use int *sam.

It just seems more natural to me that it's a pointer. Much like I would also prefix the & (addressof) directly in front of the variable.

Code:
int *sam;
int sam2;

sam = &sam2;
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Prefer:
int *p;

Doing so, removes any potential chance of what others showed (confusion)
int *p, p1;

Also, I prefer to not mix and match
int *p;
int p1;

Pretty much this, one declaration per line eliminates all of the ambiguities as well as improves readability.
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
Prefer:
int *p;

Doing so, removes any potential chance of what others showed (confusion)
int *p, p1;

Also, I prefer to not mix and match
int *p;
int p1;

I use
int* p;

If you're not going to mix and match multiple declarations on a line, what difference does it make?

I never declare multiple variables on a single line.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
i usually do int * p or int *p. soemtimes it depends on what surrounding code looks like
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
It's one of the unfortunate things about C grammar. Pretty much everyone would prefer int*, and almost nobody would actually write int* a, b, c anymore, I hope, but because it's allowed most people learn to write int *p.
 

irishScott

Lifer
Oct 10, 2006
21,562
3
0
Prefer:
int *p;

Doing so, removes any potential chance of what others showed (confusion)
int *p, p1;

Also, I prefer to not mix and match
int *p;
int p1;

Same. I've also never encountered a situation where it was advantageous to declare multiple pointers on the same line as opposed to just giving each it's own line/unique name.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
<snip>
I never declare multiple variables on a single line.
I have seen many "hotshots" out of school stack them up on a line.

either they think that extra lines are going to cost them speed, memory, storage or they have the attitude that commenting and readability are not for the prima donna.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
I haven't written C++ since a course I took on it in college (so, 7-8 years ago?); if I did now I would write "int *p". Even so, I would not declare multiple variables on a single line.
 

Broheim

Diamond Member
Feb 17, 2011
4,587
3
81
I have seen many "hotshots" out of school stack them up on a line.

either they think that extra lines are going to cost them speed, memory, storage or they have the attitude that commenting and readability are not for the prima donna.

I do it in Java if it's something really generic that I need a bunch of.
 

iCyborg

Golden Member
Aug 8, 2008
1,388
94
91
I use int*.
It's part of the type name, not variable name, so I like it better this way. But it's more of a habit than anything else. I never declare pointers and nonpointers in the same line anyway.

I also prefer 'int* const' to 'int *const'.
 

Red Squirrel

No Lifer
May 24, 2003
71,332
14,092
126
www.anyf.ca
I usually do int * p; and I never do more than one per line.

If I'm writing some parsing code or something where I have a bunch of temp variables that get reused, then I might declare on the same line. ex:

int tmpint1, tmpint2, tmpint3;
char tmpch1, tmpch2, tmpch3;
string tmpstr1, tmpstr2;
etc

But variables that serve different/dedicated purposes get their own line.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Same. I've also never encountered a situation where it was advantageous to declare multiple pointers on the same line as opposed to just giving each it's own line/unique name.
Huh? You have to give them unique names even if you put them on the same line.

I don't think declaring multiple variables per line is bad as such. If your style is good, you just rarely have multiple variables to declare at the same time, because you are declaring everything just before you need it, and also initializing it right off the bat which causes you to not want to chain more declarations afterwards. And with C++, you are especially unlikely to declare several naked pointers because you don't have many of them to go around overall.