
-----------------------------------
Andy
Tue Jul 25, 2006 5:01 am

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.

-----------------------------------
Mazer
Tue Jul 25, 2006 7:16 am


-----------------------------------
Both work, and both are right. Pick whichever works for you. Personally, I prefer the second.

-----------------------------------
Null
Tue Jul 25, 2006 10:01 pm


-----------------------------------
Which is the correct editor?
Which is the correct operating system?
Which is the correct colour shirt to wear?

-----------------------------------
r.3volved
Tue Jul 25, 2006 11:21 pm


-----------------------------------
Which is the correct editor?
Which is the correct operating system?
Which is the correct colour shirt to wear?

vi
Slackware
Camoflauge

-----------------------------------
Andy
Wed Jul 26, 2006 10:01 am


-----------------------------------
which is the correct way to be a fa9 and get your ass flamed?

-----------------------------------
Mazer
Wed Jul 26, 2006 11:24 am


-----------------------------------
Using numbers in place of letters.

Next.

-----------------------------------
bugzpodder
Sun Jul 30, 2006 10:48 pm


-----------------------------------
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]
Mon Jul 31, 2006 1:12 pm


-----------------------------------
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 [url=http://compsci.ca/v2/viewtopic.php?t=1747]here.

-----------------------------------
Null
Mon Jul 31, 2006 8:40 pm


-----------------------------------
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
Thu Aug 03, 2006 11:44 am


-----------------------------------
becareful, hes a mod and he could get your ass banned :)  (its like police officers :) )
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
Thu Aug 03, 2006 2:12 pm


-----------------------------------
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
Sun Aug 06, 2006 12:28 am


-----------------------------------
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
Sun Aug 06, 2006 10:27 am


-----------------------------------
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
Sun Aug 06, 2006 10:37 am


-----------------------------------
but through that way of thinking, you can only declare 1 pointer per line.

-----------------------------------
r.3volved
Sun Aug 06, 2006 4:53 pm


-----------------------------------
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.

-----------------------------------
Andy
Sun Aug 06, 2006 6:47 pm


-----------------------------------
err no.. under gcc, if you declare your variables as int* a, b, c; a will be a integer pointer, while b and c are integers.
