A pointer is just like any other variable. All it does is hold a value. The value a pointer holds is a memory address. You dereference the pointer to "get to" whatever is being "pointed to".
Code:
int x;
int *p;
x = 5; /* assign some value to x... */
p = &x; /* assign some value to p... */
Here you can see they work the exact same way. You simply assign them a value. However, keep in mind the type of value they each expect. One expects an int, so we give it one. The other expects the address of a variable, so we give it one.
You can also assign the value stored in one pointer to another, just like you can assign the value stored in one "normal" variable to another:
Code:
int x, int y;
int *p, *q;
x = 5;
p = &x; /* Just like before... */
y = x; /* Assign the value stored in x to y. */
q = p; /* Assign the value stored in p to q. */
Everything in C is done 'by value'. When you pass a variable to a function, it makes a copy of that variable, and passes it to the function. In other words, it passes the value stored in the variable to a function, and not the actual variable. Now consider a function call with an int argument:
Code:
int foo( int bar )
{
}
...
foo( x );
This does not send x to the function. It sends the value of x to the function. So in this case, it would send the value 5 to the function. The function creates a variable called 'bar', which is an integer, filled with the value passed to it.
Likewise, functions that pass pointers pass their values to functions. So if we had this:
Code:
void foo( int *bar )
{
}
...
foo( p );
It would take the value stored in p, which is the address of x, and stick it in a new varaiable foo, when the function is called. Since we now have the address of a variable which exists outside of the function, we can dereference it and change the value of that object. Because when you dereference a pointer, you access the variable it points to. Or rather, the variable at that address.
Quzah.