Need help with understanding pointer

I was reading my codes and i really have a hard time understand pointer because they are so confusing to me.

I would like to ask,

for example i have this declaration called
 
int *ip;


My question is when do i use ip and when do i use *ip?

Why is it
1
2
 
ip = new int; 


while
1
2
 
*ip = num; //num is some integer variable that contains a value 
Hello amoureux,

I will give this a try. If I get something wrong someone will correct me.

int *ip; declares a pointer to store the address of an integer. Knowing the type is useful later on with pointer arithmetic.

ip = new int; stores the address that new creates, since new only returns the address of the memory it sets aside when it is called. Because "ip" was declared as a pointer nothing special is needed to put the address in the variable.

*ip = num;. The * here is the says to dereference the pointer to get to the actual value stored at that particular memory location and then set the value equal to num. The same *ip is used to print the value in a cout statement whereas ip would just print the memory address.

Hope that helps,

Andy
Agree with Andy.

Best way to see this would be to print out 'ip' and '*ip" to the console so you can see the difference.

There is a very good tutorial on this site: http://www.cplusplus.com/doc/tutorial/pointers/

It's quite long, but should help you get your head around pointers, if you're new to it.
Topic archived. No new replies allowed.