...

View Full Version : I'm confused...pointers



BrickInTheWall
07-02-2009, 10:23 PM
Are these both valid pointer declarations?




int *somepointer;
int* anotherpointer;



or is there a difference between the two?

the first one is basically a dereferenced pointer right?

Cheers,
Chris

oracleguy
07-02-2009, 11:24 PM
There is no difference between those two lines functionality wise, they both do the exact same thing. And they are both valid declarations.

BrickInTheWall
07-02-2009, 11:31 PM
There is no difference between those two lines functionality wise, they both do the exact same thing. And they are both valid declarations.

ok so it's more of a personal preference thing?

Initializing them would work as follows?



int variable1 = 5;
int *somepointer = variable1;

int variable2 = 5;
int* anotherpointer = &variable2;


edit: though I do think the second way would be better when having pointers as function arguments

oracleguy
07-02-2009, 11:33 PM
ok so it's more of a personal preference thing?

Yeah, it is just a style thing.



Initializing them would work as follows?



int variable1 = 5;
int *somepointer = &variable1;

int variable2 = 5;
int* anotherpointer = &variable2;


Close, see change above.

BrickInTheWall
07-02-2009, 11:39 PM
Yeah, it is just a style thing.

Close, see change above.
thanks I tried it and it worked...it just kind of confuses me because


int *somePointer;


looks like an integer to me and I would think I could pass a normal variable to it as is looks like a dereferenced pointer.

oracleguy
07-02-2009, 11:41 PM
It might look like that in a way, but it isn't and if it bothers you, just write it with the star next to the data type. :)

BrickInTheWall
07-02-2009, 11:44 PM
I'll keep doing it the way I always did thanks...but it's good to know so I'll understand it when I see it in someone else's code...thanks for putting up with me ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum