• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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.
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 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.
 
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;
 
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;
 
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.
 
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.
 
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.
 
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.
 
<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.
 
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.
 
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.
 
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'.
 
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.
 
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.
 
Back
Top