One of the best things about pointers is that they allow functions to alter variables outside of there own scope. By passing a pointer to a function you can allow that function to read and write to the data stored in that variable. Say you want to write a function that swaps the values of two variables. Without pointers this would be practically impossible, here's how you do it with pointers:
Example 5-2. swap_ints.c
#include <stdio.h>
int swap_ints(int *first_number, int *second_number);
int
main()
{
int a = 4, b = 7;
printf("pre-swap values are: a == %d, b == %d\n", a, b)
swap_ints(&a, &b);
printf("post-swap values are: a == %d, b == %d\n", a, b)
return 0;
}
int
swap_ints(int *first_number, int *second_number)
{
int temp;
/* temp = "what is pointed to by" first_number; etc... */
temp = *first_number;
*first_number = *second_number;
*second_number = temp;
return 0;
}