🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
2 comments, last by Nothingness 22 years, 6 months ago
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?
Advertisement
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...
...go on and live with no regrets, you only have one life...
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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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 -
- Free Your Mind -

This topic is closed to new replies.

Advertisement