Pointers in C

Study your text (the Weiss book), Sections 6.1-6.5, pages 123-134.


Pointers in scanf: Study the second half of the web page about Input/Output in C, which discusses scanf and passing the address of a variable to this function so that it can insert a value into the variable. The final example of the page is the function fetch.c that shows reference parameters in action.


A function to swap values in C: Study the first half of the web page giving various functions that will interchange the values in two variables: Swapping Programs. In C this is done with the same kind of reference parameters as above.


The address operator &: You are familiar with the address operator & found in scanf("%i", &number); What this statement truly does is designates the address of where the integer should be stored. This is similar to your putting an address on a letter you are mailing.

Consider:

Address example: address.c
#include <stdio.h>

int main() {
   int a = 13;
   int b = 45;
   printf("a = %i;  address of a = %u \n", a, &a);
   printf("b = %i;  address of b = %u \n", b, &b);
}
Execution
% cc -o address address.c
% address
a = 13;  address of a = 3221222552
b = 45;  address of b = 3221222548


Pointers and dereferencing: The dereferencing operator *:

The star, *, is used for two purposes:


Baaaaaad pointer statements: Here suppose both ptr and tptr are declared as pointers to int and are both set pointing to some location.


More examples of pointers: This program gives more examples of pointers and their features.

Pointer examples: pointers.c

/* pointers.c: pointer examples */
#include <stdio.h>

int main()  {
   int x;    /* x can hold an int */
   int *xp;  /* xp is a pointer to an int */
             /* xp can hold the address of an int */
   xp = &x;  /* xp points to x; xp holds the address of x */
             /* this uses the address of operator &  */
   /* at this point xp has a value, but x is not initialized */
   *xp = 23;  /* same as setting x = 23 */
   /*  *xp refers the the location given by x */
   /*  this uses the dereference operator *  */
   /*  Notice that x == *&x  always */
   printf("x:   %i\n", x);     /* prints 23 */
   printf("*xp: %i\n", *xp); /* prints 23 */
   printf("*&x: %i\n", *&x); /* prints 23 */
   (*xp)++; /* increments what xp points to, that is, x */
   /* need parens above; *xp++ is *(xp++), increments the address */
   printf("x:   %i\n", x);     /* prints 24 */
   printf("*xp: %i\n", *xp); /* prints 24 */
   printf("*&x: %i\n", *&x); /* prints 24 */
}
Execution
% cc -o pointers pointers.c
% pointers
x:   23
*xp: 23
*&x: 23
x:   24
*xp: 24
*&x: 24

Notice that


Example of pointers to pointers to ...: Such features are seldom used in C, but one can write code with "pointers to" (repeat any number of times) x.

Pointers to pointers: ptop.c

/* ptop.c: pointers to pointers */
#include <stdio.h>

int main()  {
   /*  53 == x <--- xp <--- xpp <--- xppp <--- xpppp */
   int x;                 /* x     is an int*/
   int *xp = &x;          /* xp    is a pointer to an int*/
   int **xpp = &xp;       /* xpp   is (a pointer to)2 an int*/
   int ***xppp = &xpp;    /* xppp  is (a pointer to)3 an int*/
   int ****xpppp = &xppp; /* xpppp is (a pointer to)4 an int*/
   ****xpppp = 53;
   printf("%i %i %i %i %i\n",
      x, *xp, **xpp, ***xppp, ****xpppp);
   printf("%i %i %i %i %i\n",
      *&x, *&*xp, ***&xpp, *&*&x, ***&*&xpp, ***&*&*xppp);
   /*  printf("%i\n", **&&x);  ERROR */
}

Execution
% cc -o ptop ptop.c
% ptop
53 53 53 53 53
53 53 53 53 53

Notice that one can always write &z for any variable z, however it is declared, giving the address of z. The resulting address is a constant, and it does not have an addrss, so that &&z is always illegal.


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.