Archived

This topic is now archived and is closed to further replies.

Nothingness

When do you use the " * " and not on pointers?

Recommended Posts

Well i get how pointers work, but wheni put them in code, i get some errors, so when i take out the" * " in front of the pointer it workds. ie. int somenum, *psomenum; psomenum = &somenum; //i dont use the * why? somefunc(psomenum) //again i dont have to use it why? but some times i do have to use the " * ". So im just asking when do you use the * and when not, and why?

Share this post


Link to post
Share on other sites
I''ll try to help you.

when declaring a pointer:

int num; //normal int
int *pNum; //pointer to int

and when changing the value that is being pointed to by the pointer:

pNum = # //point to num,
*pNum = 10; //change value of num to 10

I think thats all..


...go on and live with no regrets, you only have one life...

Share this post


Link to post
Share on other sites
Look at it this way. The * and & operators are opposite of each other. The & operator tells the compiler that you have a variable, not a pointer, and you want a pointer to that address returned.(Actually it isnt quite a pointer, it''s the actual mem address, but forget that). THe * says that you have a mem address, like a pointer, and you want the variable being pointed to.

int somenum, *psomenum;

somenum = 5;
psomenum = NULL; //Doesnt point to anywhere
psomenum = &somenum; //psomenum points to somenum
cout << somenum; //Prints out "5"
cout << &somenum; //prints out some hexadecimal junk
cout << *psomenum; //prints out 5
cout << psomenum; //same hex crap as before

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
Hi, When you declare a pointer:
int *psomething;
you are saying that you want a variable of type (int *) which is a pointer to int. This pointer is nothing more than a variable that holds an address.

when you say:
psomething = &something;
you are saying I want the variable psomething(which holds an address) to hold the address of something.

when you say:
*psomething = 5;
you are saying, look at the address that psomething holds and go to that location(which is something), and put a 5 in it.

hope this helps.


- Free Your Mind -

Share this post


Link to post
Share on other sites