...

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



BrickInTheWall
07-02-2009, 11: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-03-2009, 12:24 AM
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-03-2009, 12:31 AM
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-03-2009, 12:33 AM
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-03-2009, 12:39 AM
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-03-2009, 12:41 AM
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-03-2009, 12:44 AM
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