|
|
|
View Poll Results: What syntax do you use?
|
|
int* pointer
|
  
|
9 |
37.50% |
|
int * pointer
|
  
|
3 |
12.50% |
|
int *pointer
|
  
|
11 |
45.83% |
|
More than one
|
  
|
1 |
4.17% |
 |
12-04-2012, 12:19 AM
|
#1
|
|
Lifer
Join Date: Oct 2006
Location: Delaware
Posts: 17,786
|
Poll: How do you declare c/c++/c# style pointers?
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"
|
|
|
12-04-2012, 01:02 AM
|
#2
|
|
Senior Member
Join Date: Dec 2007
Posts: 929
|
What are the types of the following?
int* i, j, k;
Thus, i do int *p;
__________________
Intel Core i7 860 (Thermalright 120 Ultra) | MSI P55-GD65 | 4x4GB DDR3-1600 | XFX Radeon 7950 | WD 640GB Black & 4x Samsung EcoGreen F2 1.5TB (RAID-5) & Samsung F4 1TB | Antec Neo Eco 620c | Logitech Z-5500 | Antec 300 | Dell Ultrasharp 2312HM & Dell Ultrasharp 2007WFP | Logitech MX518 | Windows 7 Professional x64
Last edited by Absolution75; 12-04-2012 at 01:04 AM.
|
|
|
12-04-2012, 01:03 AM
|
#3
|
|
Golden Member
Join Date: Feb 2008
Posts: 1,468
|
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.
|
|
|
12-04-2012, 08:21 AM
|
#4
|
|
Administrator Discussion Club Moderator Elite Member
Join Date: Oct 2000
Posts: 39,867
|
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;
__________________
F15 Air Superiority Fighter - Never has one been lost in aerial combat (104 kills)
|
|
|
12-04-2012, 09:20 AM
|
#5
|
|
Platinum Member
Join Date: Oct 2006
Posts: 2,620
|
Quote:
Originally Posted by irishScott
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;
__________________
Computer Setup
Intel Ivy Bridge 3550, AsRock Z75 Pro3, 8 gig GSkill 1600 8-8-8-24 timing, Crucial m4 64gig, 500meg WD RE4, ATI 7970 3GB, LG IPS236V x 3, Windows 8, Antec Sonata Solo 2, Antec 550watt PS.
|
|
|
12-04-2012, 09:24 AM
|
#6
|
|
Lifer
Join Date: Sep 2001
Location: ATX
Posts: 11,947
|
Quote:
Originally Posted by EagleKeeper
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.
__________________
Consequences will never be the same!
/^1?$|^(11+?)\1+$/
|
|
|
12-04-2012, 09:27 AM
|
#7
|
|
Golden Member
Join Date: Oct 2007
Location: Texas
Posts: 1,007
|
Quote:
Originally Posted by EagleKeeper
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.
|
|
|
12-04-2012, 09:36 AM
|
#8
|
|
Diamond Member
Join Date: Sep 2005
Posts: 7,273
|
i usually do int * p or int *p. soemtimes it depends on what surrounding code looks like
__________________
Sushi is good
Quote:
Originally Posted by brianmanahan
smoblikat you fool, you fell victim to one of the classic blunders! the most famous of which is "never post your actual pay in an ATOT brag thread", but only slightly less well known is this: "never post your actual pay when alkemyst is online!!" hahahahaha
|
|
|
|
12-04-2012, 10:26 AM
|
#9
|
|
Moderator Programming
Join Date: Sep 2005
Posts: 8,154
|
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.
|
|
|
12-04-2012, 11:59 AM
|
#10
|
|
Lifer
Join Date: Oct 2006
Location: Delaware
Posts: 17,786
|
Quote:
Originally Posted by EagleKeeper
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.
|
|
|
12-04-2012, 12:22 PM
|
#11
|
|
Administrator Discussion Club Moderator Elite Member
Join Date: Oct 2000
Posts: 39,867
|
Quote:
Originally Posted by veri745
<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.
__________________
F15 Air Superiority Fighter - Never has one been lost in aerial combat (104 kills)
|
|
|
12-04-2012, 01:23 PM
|
#12
|
|
Lifer
Join Date: Feb 2003
Posts: 20,488
|
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.
|
|
|
12-04-2012, 02:17 PM
|
#13
|
|
Senior Member
Join Date: Sep 2002
Posts: 872
|
Does Objective-C count?
NSString *name = @"Jim";
|
|
|
12-04-2012, 02:43 PM
|
#14
|
|
Diamond Member
Join Date: Feb 2011
Location: with my auntie and uncle in Bel Air
Posts: 3,043
|
Quote:
Originally Posted by EagleKeeper
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.
__________________
Quote:
Originally Posted by free ipod
It's Diffiult Process but you handle easily. I am Appriciate with your Perforamances...
|
|
|
|
12-04-2012, 05:35 PM
|
#15
|
|
Senior Member
Join Date: Aug 2008
Posts: 957
|
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'.
|
|
|
12-04-2012, 06:31 PM
|
#16
|
|
Lifer
Join Date: May 2003
Location: Canada
Posts: 20,690
|
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.
__________________
~Red Squirrel~
That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved. For it is with your heart that you believe and are justified, and it is with your mouth that you confess and are saved. Romans 10:9-10
|
|
|
12-04-2012, 07:23 PM
|
#17
|
|
Golden Member
Join Date: Mar 2008
Posts: 1,389
|
Quote:
Originally Posted by Crusty
Pretty much this, one declaration per line eliminates all of the ambiguities as well as improves readability.
|
This.
|
|
|
12-04-2012, 08:37 PM
|
#18
|
|
Golden Member
Join Date: Feb 2008
Posts: 1,468
|
Quote:
Originally Posted by irishScott
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.
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:07 AM.
|