|
Quick Navigation Bar input/output and file i/o :: pointers :: arrays [ toc | forums ] |
Note: If the document URL does not begin with http://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
STOP! If you have not gone over Section 1: Simple C or do not completely understand what is going on, do not proceed. The following will make absolutely no sense if you have not read Section 1. Go back and re-read pages that are troubling you and practice before proceeding! If you are comfortable with the material discussed thus far, lets begin our journey into pointers...
* in front of the
variable identifier. For example:
int *ip; float *fp = NULL;This delcares a pointer, ip, to an integer. Let's say we want ip to point to an integer. The second line delares a pointer to a float, but initializes the pointer to point to the
NULL pointer.
The NULL pointer points to a place in memory that cannot
be accessed. NULL is useful when checking for error
conditions and many functions return NULL if they fail.
int x = 5; int *ip; ip = &x;We first encountered the
& operator first in the I/O
section. The & operator is to specify the address-of
x. Thus, the pointer, ip is pointing to x by assigning the address
of x. This is important. You must understand this concept.* operator. The *
dereferences the pointer to the value. So,
printf("%d %d\n", x, *ip);
would print 5 5 to the screen.int x = 0, y = 5, *ip = &y; x = *ip;The statement
int *ip = &y; is different than
x = *ip;. The first statement does not dereference,
the * signifies to create a pointer to an int. The second
statement uses a dereference.
void swap(int *x, int *y) {
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int main() {
int a = 2, b = 3;
swap(&a, &b);
return EXIT_SUCCESS;
}
This snip of swapping code works. When you call swap, you must
give the address-of a and b, because swap is expecting a pointer.const type qualifier can make things a little
confusing when it is used with pointer declarations.
const int * const ip; /* The pointer *ip is const and what it points at is const */
int * const ip; /* The pointer *ip is const */
const int * ip; /* What *ip is pointing at is const */
int * ip; /* Nothing is const */
As you can see, you must be careful when specifying the
const qualifier when using pointers.void pointers can be assigned to any pointer value.
It sometimes necessary to store/copy/move pointers without regard
to the type it references.void pointer.malloc, free, and
scanf utilize void pointers.int x = 5, *ip = &x; ip++;On a typical 32-bit machine, *ip would be pointing to 5 after initialization. But
ip++; increments the pointer 32-bits
or 4-bytes. So whatever was in the next 4-bytes, *ip would be pointing
at it.& and *.
The & operator gives the address-of a pointer. The
* dereferences the pointer (when not used in a pointer
declaration statement).const type qualifier.
You have to also be cautious about the void pointer.
