Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

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%
Voters: 24. You may not vote on this poll

Reply
 
Thread Tools
Old 12-04-2012, 12:19 AM   #1
irishScott
Lifer
 
irishScott's Avatar
 
Join Date: Oct 2006
Location: Delaware
Posts: 20,471
Default 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"
irishScott is offline   Reply With Quote
Old 12-04-2012, 01:02 AM   #2
Absolution75
Senior Member
 
Join Date: Dec 2007
Posts: 969
Default

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.
Absolution75 is offline   Reply With Quote
Old 12-04-2012, 01:03 AM   #3
Pia
Golden Member
 
Join Date: Feb 2008
Posts: 1,534
Default

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.
Pia is offline   Reply With Quote
Old 12-04-2012, 08:21 AM   #4
EagleKeeper
Discussion Club Moderator
Elite Member
 
EagleKeeper's Avatar
 
Join Date: Oct 2000
Location: Bumps west of Denver
Posts: 42,600
Default

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)
EagleKeeper is offline   Reply With Quote
Old 12-04-2012, 09:20 AM   #5
brandonb
Diamond Member
 
Join Date: Oct 2006
Posts: 3,447
Default

Quote:
Originally Posted by irishScott View Post
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.
brandonb is offline   Reply With Quote
Old 12-04-2012, 09:24 AM   #6
Crusty
Lifer
 
Crusty's Avatar
 
Join Date: Sep 2001
Location: ATX
Posts: 12,316
Default

Quote:
Originally Posted by EagleKeeper View Post
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+$/
Crusty is offline   Reply With Quote
Old 12-04-2012, 09:27 AM   #7
veri745
Golden Member
 
veri745's Avatar
 
Join Date: Oct 2007
Location: Texas
Posts: 1,104
Default

Quote:
Originally Posted by EagleKeeper View Post
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.
__________________
Heat
veri745 is online now   Reply With Quote
Old 12-04-2012, 09:36 AM   #8
TecHNooB
Diamond Member
 
TecHNooB's Avatar
 
Join Date: Sep 2005
Posts: 7,438
Default

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 View Post
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
TecHNooB is offline   Reply With Quote
Old 12-04-2012, 10:26 AM   #9
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,362
Default

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.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is online now   Reply With Quote
Old 12-04-2012, 11:59 AM   #10
irishScott
Lifer
 
irishScott's Avatar
 
Join Date: Oct 2006
Location: Delaware
Posts: 20,471
Default

Quote:
Originally Posted by EagleKeeper View Post
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.
irishScott is offline   Reply With Quote
Old 12-04-2012, 12:22 PM   #11
EagleKeeper
Discussion Club Moderator
Elite Member
 
EagleKeeper's Avatar
 
Join Date: Oct 2000
Location: Bumps west of Denver
Posts: 42,600
Default

Quote:
Originally Posted by veri745 View Post
<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)
EagleKeeper is offline   Reply With Quote
Old 12-04-2012, 01:23 PM   #12
clamum
Lifer
 
clamum's Avatar
 
Join Date: Feb 2003
Posts: 21,552
Default

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.
__________________
\ Battlefield 4 Stats /\ Heat /
clamum is offline   Reply With Quote
Old 12-04-2012, 02:17 PM   #13
mosco
Senior Member
 
Join Date: Sep 2002
Posts: 929
Default

Does Objective-C count?

NSString *name = @"Jim";
mosco is offline   Reply With Quote
Old 12-04-2012, 02:43 PM   #14
Broheim
Diamond Member
 
Broheim's Avatar
 
Join Date: Feb 2011
Location: with my auntie and uncle in Bel Air
Posts: 4,569
Default

Quote:
Originally Posted by EagleKeeper View Post
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 View Post
It's Diffiult Process but you handle easily. I am Appriciate with your Perforamances...
Broheim is offline   Reply With Quote
Old 12-04-2012, 05:35 PM   #15
iCyborg
Golden Member
 
Join Date: Aug 2008
Posts: 1,039
Default

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'.
iCyborg is offline   Reply With Quote
Old 12-04-2012, 06:31 PM   #16
Red Squirrel
Lifer
 
Red Squirrel's Avatar
 
Join Date: May 2003
Location: Canada
Posts: 27,284
Default

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~
486dx2 @66Mhz turbo, 8MB ram, 512MB HDD, sound blaster 16 + 2x cdrom, Trident 1MB video card @ 640*480, 56k high speed modem.
Red Squirrel is online now   Reply With Quote
Old 12-04-2012, 07:23 PM   #17
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

Quote:
Originally Posted by Crusty View Post
Pretty much this, one declaration per line eliminates all of the ambiguities as well as improves readability.
This.
degibson is offline   Reply With Quote
Old 12-04-2012, 08:37 PM   #18
Pia
Golden Member
 
Join Date: Feb 2008
Posts: 1,534
Default

Quote:
Originally Posted by irishScott View Post
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.
Pia is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.