Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 pointers
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

How do you declare your pointers?
(No ending time set)
int* a;
14%
 14%  [ 2 ]
int *a;
85%
 85%  [ 12 ]
Total Votes : 14

Author Message
Andy




PostPosted: Tue Jul 25, 2006 5:01 am   Post subject: pointers

just out of curiosity, what is the correct way of declaring a pointer?

int* a;
or
int *a;

for the longest time i've been using int* a, that makes more sense to me since you're declaring a to be an int pointer.

But i suppose int *a works, since this is saying that the memory a points to is of type an integer.

I'm leaning more towards the second one otherwise int a, *b; wouldnt make sense.
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Tue Jul 25, 2006 7:16 am   Post subject: (No subject)

Both work, and both are right. Pick whichever works for you. Personally, I prefer the second.
Null




PostPosted: Tue Jul 25, 2006 10:01 pm   Post subject: (No subject)

Which is the correct editor?
Which is the correct operating system?
Which is the correct colour shirt to wear?
r.3volved




PostPosted: Tue Jul 25, 2006 11:21 pm   Post subject: (No subject)

Null wrote:
Which is the correct editor?
Which is the correct operating system?
Which is the correct colour shirt to wear?


vi
Slackware
Camoflauge
Andy




PostPosted: Wed Jul 26, 2006 10:01 am   Post subject: (No subject)

which is the correct way to be a fa9 and get your ass flamed?
Mazer




PostPosted: Wed Jul 26, 2006 11:24 am   Post subject: (No subject)

Using numbers in place of letters.

Next.
bugzpodder




PostPosted: Sun Jul 30, 2006 10:48 pm   Post subject: (No subject)

acutally, there are problems as noted, like
int a, *b,
and as well declaring array of pointers
i am not sure what works and what doesnt but you have to experiment with things. like differences between
char []a
char[] a
char a[]

and when it comes to array of pointers where do you put the start etc
[Gandalf]




PostPosted: Mon Jul 31, 2006 1:12 pm   Post subject: (No subject)

Though clearly there is no definate answer, I would say:
int A, *B - B is a pointer to an integer
char[] A - A is an array of characters (not an array of As?)

Anyway, yeah, I can definately agree with the fact that all this notation can get very complicated later on. For example here.
Sponsor
Sponsor
Sponsor
sponsor
Null




PostPosted: Mon Jul 31, 2006 8:40 pm   Post subject: (No subject)

Andy wrote:
which is the correct way to be a fa9 and get your ass flamed?


I think you need to grow up, and realize that I am atleast 2 years younger then you yet I avoid the use of childish insults.

If you were not so quick to be defensive, you would see that I was making a point relavent to your question. The issue of where to put the '*' for pointers is subjective to the programmer, and simply a matter of style and preference (just as the choice of OS, shirt colour, and editor is).
bugzpodder




PostPosted: Thu Aug 03, 2006 11:44 am   Post subject: (No subject)

becareful, hes a mod and he could get your ass banned Smile (its like police officers Smile )
anyways give him a break... hes stressed out from all the classes and exams (you'd be surprised how much harder it is to study in university than in high school, since we cover twice amount of materials in half the time), probably from too much video games.
McKenzie




PostPosted: Thu Aug 03, 2006 2:12 pm   Post subject: (No subject)

You missed one

int * a;

as used by cplusplus.com
http://www.cplusplus.com/doc/tutorial/pointers.html

I think most of the C++ community, including Bjarne Stroustrup uses
int* a;

I prefer:
int *a;

i think mainly because that's how I learned it. I'm pretty sure that those whom use the former prefer it because it trys to make it clear that it is not the same as dereferencing. Because it is related to dereferencing I don't find the latter to be a problem. I may have to change my style one day to be more mainsteam. Despite what some have said I think it is important to code in as standard of a way as possible.
Panopticon




PostPosted: Sun Aug 06, 2006 12:28 am   Post subject: (No subject)

r.3volved wrote:
Null wrote:
Which is the correct editor?
Which is the correct operating system?
Which is the correct colour shirt to wear?


vi
Slackware
Camoflauge



emacs
Gentoo
hot pink
r.3volved




PostPosted: Sun Aug 06, 2006 10:27 am   Post subject: (No subject)

lol

I use the format int* a; as the way I think about it in my head as I'm coding is that I'm declairing a as type integer pointer rather than saying I'm declairing pointer a as type integer.

Just makes more sense to me that way and a lot less confusing in large code chunks. Then you use the -> operator for the majority of code instead of always dereferencing as (*a)
Andy




PostPosted: Sun Aug 06, 2006 10:37 am   Post subject: (No subject)

but through that way of thinking, you can only declare 1 pointer per line.
r.3volved




PostPosted: Sun Aug 06, 2006 4:53 pm   Post subject: (No subject)

int* a,b,c;
will declair a, b and c each as integer pointers

int *a, *b, *c;
will also do the same

int *a, *b, c;
will also compile successfully under g++, though I didn't test functionality

I will rarely ever declair multiple values on a single line unless they are related, such as x,y and z co-ordinates. It makes things far too confusing and often times you end up declairing too many datatypes before you really need to use them.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: