Integer Pointers
A simple example that shows what a pointer is.
Source Code:
(main.c)
#include <stdio.h>
int main(){
int a,b; /* declare integers */
int *ptr_a; /* declare a pointer to an integer */
a=3;
ptr_a=&a; /*initialize ptr_a to be a pointer to a */
/* &a returns the address of a in memory */
printf("1) ptr_a = %p\n", ptr_a); /* Print out the value of ptr_a */
/* Here we print a location in memory */
printf("2) *ptr_a = %d\n", *ptr_a); /* (*ptr_a) returns the value of the
integer that ptr_a points to */
printf("3) 3*(*ptr_a)+2 = %d\n", 3*(*ptr_a)+2); /* *ptr_a can be used
like any other integer.*/
a=7;
printf("4) *ptr_a = %d\n", *ptr_a); /* The value of (*ptr_a) also changes! */
b=14;
ptr_a=&b; /* Now we change ptr_a so it points to b. */
printf("5) *ptr_a = %d\n", *ptr_a);
return 0;
}
Output:
1) ptr_a = 0xbffff484
2) *ptr_a = 3
3) 3*(*ptr_a)+2 = 11
4) *ptr_a = 7
5) *ptr_a = 14
To compile: