OK,
head
is declared as a
node*
which means that
head
is a variable which contains a pointer to a
node
object.
Think about cars for a moment. You have a space in the car park where you have parked your car. If you let me drive it, you don't tell me "it's the red Ford Fiesta, registration number ABC 123" because I'd have to search the whole car park looking for it - you tell me it's parked on level 3, row B, bay 17: 3B17 is enough for me to find the car.
"3B17" is the address of the bay containing the car - or a pointer to the car. If I move the car, I return a different address: "4A12" and you can find the car and go home.
Similarly,
head
doesn't contain the first node in your list - it contains a number which tells you where that node is: the address of the node. And just like I did, I found your car by going to that address.
To go to the node, you follow the pointer - this is called
dereferencing the pointer - by prefixing it with a '*' or by using the -> operator.
To get the address of anything, you prefix it with '&': so
&head
gives you the address of
head
, or a pointer to a pointer to a node.
And in C, all values are passed to functions by value (not by reference) which means that inside the function you use a copy of what is passed - any changes you make do not affect the outside world. This is handy:
void DoIt(int i)
{
i = 2 * i;
}
Is fine when you do this:
int i = 666;
DoIt(i);
printf(i);
And you get the value 666 printed. If it passed by reference, you would print 1332 - which would also work, until you did this:
DoIt(666);
Passing by value means that the function can't affect the outside world, so there is no attempt to change fundamental constants!
So when you write:
Push(&head, 1);
You pass through the address of head, or a pointer to head (or a pointer to a pointer to a node) which means that head itself can be changed by the function.
When you use your
foo
function, a copy of the address of the 'c' in "cat" is passed to the function (by value, remember), the function changes the copy by auto incrementing it, and the copy is discarded at the end of the function, leaving the outside world unchanged.