Previous: 7.3 Arrays, Pointers, Pointer Arithmetic
Up: 7.3 Arrays, Pointers, Pointer Arithmetic
Next: 7.3.2 Array Names vs Pointer Variables
Previous Page: 7.3 Arrays, Pointers, Pointer Arithmetic
Next Page: 7.3.2 Array Names vs Pointer Variables
We have just seen that an array name, e.g. aa, is a pointer to the array and that aa + i points to aa[i]. We can illustrate this point in the program below, where the values of pointers themselves are printed. A pointer value is a byte address and is printed as an unsigned integer (using conversion specification for unsigned integer, %u). The program shows the relationships between array elements, pointers, and pointer arithmetic.
/* File: arayptr.c
This program shows the relation between arrays and pointers.
*/
#include <stdio.h>
#define N 5
main()
{ int i, j, aa[N];
printf("***Pointers, Arrays, and Pointer Arithmetic***\n\n");
for (i = 0; i < N; i++) {
aa[i] = i * i;
printf("aa + %d = %u; &aa[%d] = %u\n", i, aa + i, i, &aa[i]);
printf("*(aa + %d) = %d; aa[%d] = %d\n", i, *(aa + i), i, aa[i]);
}
}
In the loop, we first assign a value to each aa[i].
We then print values to
show that pointers, aa + i and &aa[i] are the same,
i.e. that aa + i points to aa[i].
Next, we print the array element values to show that
*(aa + i) is the same as aa[i]. A
sample output for the program is shown below:
The next example shows that pointers may be incremented and decremented. In either case, if the original pointer points to an object of a specific type, the new pointer points to the next or previous object of the same type, i.e. pointers are incremented or decremented in steps of the object size that the pointer points to. Thus, it is possible to traverse an array starting from a pointer to any element in the array. Consider the code:
/* File: arayptr2.c
Pointers and pointer arithmetic.
*/
#include <stdio.h>
#define N 5
main()
{ float faray[N], *fptr;
int *iptr, iaray[N], i;
/* initialize */
for (i = 0; i < N; i++) {
faray[i] = 0.3;
iaray[i] = 1;
}
/* initialize fptr to point to element faray[3] */
fptr = &faray[3];
*fptr = 1.; /* faray[3] = 1. */
*(fptr - 1) = .9; /* faray[2] = .9 */
*(fptr + 1) = 1.1; /* faray[4] = 1.1 */
/* initialize iptr in the same way */
iptr = &iaray[3];
*iptr = 0;
*(iptr - 1) = -1;
*(iptr + 1) = 2;
for (i = 0; i < N; i++) {
printf("faray[%d] = %f ", i, *(faray + 1));
printf("iaray[%d] = %d\n", i, iaray[i]);
}
}
The program is straightforward. It declares a float array of size 5, and an
integer array of the same size. The float array elements are all initialized to
0.3, and the integer array elements to 1. The program also declares two pointer
variables, one a float pointer and the other an integer pointer. Each pointer
variable is initialized to point to the array element with index 3; for
example, fptr is initialized to point to the float array
element, faray[3].
Therefore, fptr - 1 points to faray[2],
and fptr + 1 points to faray[4]. The value
of *fptr is then modified,
as is the value of *(fptr - 1) and *(fptr + 1).
Similar changes are made in the integer array.
Finally, the arrays are printed.
Here is the output of the program: