/* Introduction
to program:
Program
- point2.c.
Example
of the use of pointers and arrays.
Francis
O'Donovan 16-1-98. */
/* Files to be included. */
#include <stdio.h>
/* Function: main(). */
main()
{
/*
Initialize variables. */
int i; /*
Counter for for loop. */
int
j; /* Used
in for loop. */
int
z[10]; /* An integer array of size
10. */
int
*p; /* A pointer
to an integer. */
/* Print introduction on screen. */
printf( "Program: point2.c.\n" );
printf(
"Example of the use of pointers and arrays.\n" );
printf(
"Francis O'Donovan 16-1-98.\n\n" );
/* Initialize array. */
printf( "** The initial array values, " );
printf(
"initialized using a for loop **\n\n" );
for(
i = 0; i<10; i++ )
{
z[i] = i+5; /* z[0] = 5, z[1]
= 6,..., z[9] = 14. */
/* Print out the value and address of the ith element. */
printf( "The value of z[%d] is %d. ", i, z[i] );
printf( "The address of z[%d] is %d.\n", i, &(z[i])
);
}
/* Set p to point to the array. */
p = z;
/* Print out value and address of p. */
printf( "\n** Pointer p **\n\n" );
printf(
"The value of p is %d, and its address is %d.\n", p,
&p );
printf(
"The value of p is equal to the address of z[0].\n"
);
printf(
"This is because setting a pointer to point to an array means
setting " );
printf(
"it to point to the array's first element.\n\n" );
/*
Modify array to 7 through 16, using pointer. */
for ( i = 0; i<10; i++ )
{
*p = i+7; /* z[0] = 7, z[1] = 8, etc..
*/
p++;
}
/* Print out new values in array. */
printf(
"** The new array values **\n\n" );
for(
i = 0; i<10; i++ )
{
printf( "The value of z[%d] is %d.\n", i, z[i] );
}
}
© Francis O'Donovan
1999.