In C/C++, pointers and arrays are closely related. As you know, an array name
without an index is a pointer to the first element in the array. For example, consider the
following array.
char p[10];
The following statements are identical:
without an index is a pointer to the first element in the array. For example, consider the
following array.
char p[10];
The following statements are identical:
p
&p[0]
Put another way,
p == &p[0]
evaluates to true because the address of the first element of an array is the same as the
address of the array.
As stated, an array name without an index generates a pointer. Conversely, a
pointer can be indexed as if it were declared to be an array. For example, consider this
program fragment:
int *p, i[10];
p = i;
p[5] = 100; /* assign using index */
*(p+5) = 100; /* assign using pointer arithmetic */
Both assignment statements place the value 100 in the sixth element of i. The first
statement indexes p; the second uses pointer arithmetic. Either way, the result is the
same. (Chapter 5 discusses pointers and pointer arithmetic.)
This same concept also applies to arrays of two or more dimensions. For example,
assuming that a is a 10-by-10 integer array, these two statements are equivalent:
a
&a[0][0]
Furthermore, the 0,4 element of a may be referenced two ways: either by array
indexing, a[0][4], or by the pointer, *((int *)a+4). Similarly, element 1,2 is either a[1][2]
or *((int *)a+12). In general, for any two-dimensional array
a[j][k] is equivalent to *((base-type *)a+(j*row length)+k)
The cast of the pointer to the array into a pointer of its base type is necessary in order
for the pointer arithmetic to operate properly. Pointers are sometimes used to access
arrays because pointer arithmetic is often faster than array indexing.
A two-dimensional array can be reduced to a pointer to an array of onedimensional
arrays. Therefore, using a separate pointer variable is one easy way
to use pointers to access elements within a row of a two-dimensional array. The
following function illustrates this technique. It will print the contents of the specified
row for the global integer array num:
&p[0]
Put another way,
p == &p[0]
evaluates to true because the address of the first element of an array is the same as the
address of the array.
As stated, an array name without an index generates a pointer. Conversely, a
pointer can be indexed as if it were declared to be an array. For example, consider this
program fragment:
int *p, i[10];
p = i;
p[5] = 100; /* assign using index */
*(p+5) = 100; /* assign using pointer arithmetic */
Both assignment statements place the value 100 in the sixth element of i. The first
statement indexes p; the second uses pointer arithmetic. Either way, the result is the
same. (Chapter 5 discusses pointers and pointer arithmetic.)
This same concept also applies to arrays of two or more dimensions. For example,
assuming that a is a 10-by-10 integer array, these two statements are equivalent:
a
&a[0][0]
Furthermore, the 0,4 element of a may be referenced two ways: either by array
indexing, a[0][4], or by the pointer, *((int *)a+4). Similarly, element 1,2 is either a[1][2]
or *((int *)a+12). In general, for any two-dimensional array
a[j][k] is equivalent to *((base-type *)a+(j*row length)+k)
The cast of the pointer to the array into a pointer of its base type is necessary in order
for the pointer arithmetic to operate properly. Pointers are sometimes used to access
arrays because pointer arithmetic is often faster than array indexing.
A two-dimensional array can be reduced to a pointer to an array of onedimensional
arrays. Therefore, using a separate pointer variable is one easy way
to use pointers to access elements within a row of a two-dimensional array. The
following function illustrates this technique. It will print the contents of the specified
row for the global integer array num:
int num[10][10];
.
.
.
void pr_row(int j)
{
int *p, t;
p = (int *) &num[j][0]; /* get address of first
element in row j */
for(t=0; t<10; ++t) printf("%d ", *(p+t));
}
You can generalize this routine by making the calling arguments be the row, the row
length, and a pointer to the first array element, as shown here:
void pr_row(int j, int row_dimension, int *p)
{
int t;
p = p + (j * row_dimension);
for(t=0; t<row_dimension; ++t)
printf("%d ", *(p+t));
}
.
.
.
void f(void)
{
int num[10][10];
pr_row(0, 10, (int *) num); /* print first row */
}
Arrays of greater than two dimensions may be reduced in a similar way. For
example, a three-dimensional array can be reduced to a pointer to a two-dimensional
array, which can be reduced to a pointer to a single-dimension array. Generally, an
n-dimensional array can be reduced to a pointer and an (n-1)-dimensional array. This
new array can be reduced again with the same method. The process ends when a
single-dimension array is produced.
.
.
.
void pr_row(int j)
{
int *p, t;
p = (int *) &num[j][0]; /* get address of first
element in row j */
for(t=0; t<10; ++t) printf("%d ", *(p+t));
}
You can generalize this routine by making the calling arguments be the row, the row
length, and a pointer to the first array element, as shown here:
void pr_row(int j, int row_dimension, int *p)
{
int t;
p = p + (j * row_dimension);
for(t=0; t<row_dimension; ++t)
printf("%d ", *(p+t));
}
.
.
.
void f(void)
{
int num[10][10];
pr_row(0, 10, (int *) num); /* print first row */
}
Arrays of greater than two dimensions may be reduced in a similar way. For
example, a three-dimensional array can be reduced to a pointer to a two-dimensional
array, which can be reduced to a pointer to a single-dimension array. Generally, an
n-dimensional array can be reduced to a pointer and an (n-1)-dimensional array. This
new array can be reduced again with the same method. The process ends when a
single-dimension array is produced.
No comments:
Post a Comment