Home Contents


/*      Introduction to program:
        Program - point1.c.
        Example of the use of pointers in a program.
        Francis O'Donovan 12-1-98. */

/* Files to be included. */
 
#include <stdio.h>

/*      Function: main(). */

main()
{
        /* Initialize variables. */

        int x, y;       /* Two integers. */
        int z[10];      /* An array of size 10 */
        int *p;         /* A pointer to an integer. */

        /* Print introduction. */

        printf( "Program: point1.c.\n" );
        printf( "Example of the use of pointers.\n" );
        printf( "Francis O'Donovan 12-1-98.\n\n" );
 
        /* Set x to 1 and y to 2. */

        x = 1;
        y = 2;

        /* Print out values and address of x, y, z[0], and p. */

        printf( "** Initial values **\n\n" );
        printf( "The value of x is %d and its address is %d.\n", x, &x );
        printf( "The value of y is %d and its address is %d.\n", y, &y );
        printf( "The value of z[0] is %d and its address is %d.\n", z[0], &z[0] );
        printf( "The value of p is %d and its address is %d.\n", p, &p );

        /* Set p to point to x, and print out value and address of p. */

        p = &x;

        printf( "\n** p's values after p is set to &x **\n\n");

        printf( "The value of p is %d and its address is %d.\n", p, &p );
 
        /* Explain the changes in these values. */

        printf( "The value of p has been changed from an uninitialized value to the address of x.");
        printf( "This is because p now points to x.\n" );
        printf( "p's address has not been changed.\n\n");

        /* Set y to what p points to and print value and address of p. */

        y = *p;

        printf( "** y's values after y is set to *p **\n\n" );
 
        printf( "The value of y is %d and its address is %d.\n", y, &y );

        /* Explain the changes. */
 
        printf( "The value of y has been changed from 2 to the value of x (1).\n" );
        printf( "This is because it was changed to the value of what p pointed to.\n" );
        printf( "Its address has not changed. ");

        /* Set x to 0 and print out values for p and x. */

        *p = 0;

        printf( "\n\n** Values after *p (ie. x) is set to 0 **\n\n" );

        printf( "The value of x is %d and its address is %d.\n", x, &x );
        printf( "The value of p is %d and its address is %d.\n", p, &p );

        /* Explain the changes. */

        printf( "The value of x has been changed to 0.\n" );
        printf( "This is because the value of what p pointed to (i.e. x) was changed to 0.\n" );
        printf( "No other value has changed.\n\n" );
 
        /* Set p to point to z[0], print values and explain changes. */

        p = &(z[0]);

        printf( "** Values after p is set to point to z[0] **\n\n" );

        printf( "The value of z[0] is %d and its address is %d.\n", z[0], &z[0] );
        printf( "The value of p is %d and its address is %d.\n", p, &p );

        printf( "The value of p has changed to the address of z[0], as it now points to z[0].\n");
        printf( "No other value has changed.\n" );
 
        }

 


Home Contents



© Francis O'Donovan 1999.