tags:

views:

110

answers:

4
+1  A: 

Once stackPtr is passed in (by value), it is a local (automatic) variable, and modifying it doesn't affect any of the caller's automatic variables. However, when you do:

*stackPtr = node;

you're modifying the object it points to.

Matthew Flaschen
+2  A: 
*stackPtr = node;

This dereferences stackPtr and sets the object pointed at to the value of node.

stackPtr = &node;

This changes the pointer stackPtr to point to the node pointer which is allocated on the stack.

Basically, in the first case, you are changing the thing that the pointer refers to (called the referent of the pointer) but the pointer itself remains unchanged. In the second case, you are changing the pointer to refer to something different.

James McNellis
A: 

*stackPtr = node

makes stackPtr point to what you've malloc'd.

*stackPtr = &node

makes stackPtr point to the address of a local variable, which will most likely be invalid once you return from this function.

+2  A: 
Amarghosh
lets say we have int k=4; if I enter something like *ptr = k; in the "main" body (not inside a function), the results should be the same as ptr = ?
aminfar
@aminfar not exactly. See my updated answer
Amarghosh