Professional Documents
Culture Documents
6.1: I had the definition char a[6] in one source file, and in
another I declared extern char *a. Why didn't it work?
A: The declaration extern char *a simply does not match the actual
definition. The type pointer-to-type-T is not the same as array-
of-type-T. Use extern char a[].
References: ANSI Sec. 3.5.4.2; ISO Sec. 6.5.4.2; CT&P Sec. 3.3
pp. 33-4, Sec. 4.5 pp. 64-5.
6.2: But I heard that char a[] was identical to char *a.
References: K&R2 Sec. 5.5 p. 104; CT&P Sec. 4.5 pp. 64-5.
p = a;
then p[3] and a[3] would access the same element.
References: K&R1 Sec. 5.3 pp. 93-6; K&R2 Sec. 5.3 p. 99; ANSI
Sec. 3.2.2.1, Sec. 3.3.2.1, Sec. 3.3.6; ISO Sec. 6.2.2.1,
Sec. 6.3.2.1, Sec. 6.3.6; H&S Sec. 5.4.1 p. 124.
f(a)
char a[];
{ ... }
f(a)
char *a;
{ ... }
A: The type.
#include
int *dynarray = (int *)malloc(10 * sizeof(int));
(and if the call to malloc() succeeds), you can reference
dynarray[i] (for i from 0 to 9) just as if dynarray were a
conventional, statically-allocated array (int a[10]). See also
question 6.16.
#include
You can keep the array's contents contiguous, while making later
reallocation of individual rows difficult, with a bit of
explicit pointer arithmetic:
int (*array4)[NCOLUMNS] =
(int (*)[NCOLUMNS])malloc(nrows * sizeof(*array4));
but the syntax starts getting horrific and at most one dimension
may be specified at run time.
int realarray[10];
int *array = &realarray[-1];
References: K&R2 Sec. 5.3 p. 100, Sec. 5.4 pp. 102-3, Sec. A7.7
pp. 205-6; ANSI Sec. 3.3.6; ISO Sec. 6.3.6; Rationale
Sec. 3.2.2.3.
A: The rule (see question 6.3) by which arrays decay into pointers
is not applied recursively. An array of arrays (i.e. a two-
dimensional array in C) decays into a pointer to an array, not a
pointer to a pointer. Pointers to arrays can be confusing, and
must be treated carefully; see also question 6.13. (The
confusion is heightened by the existence of incorrect compilers,
including some old versions of pcc and pcc-derived lints, which
improperly accept assignments of multi-dimensional arrays to
multi-level pointers.)
int array[NROWS][NCOLUMNS];
f(array);
f(int a[][NCOLUMNS])
{ ... }
or
References: K&R1 Sec. 5.10 p. 110; K&R2 Sec. 5.9 p. 113; H&S
Sec. 5.4.3 p. 126.
6.19: How do I write functions which accept two-dimensional arrays
when the "width" is not known at compile time?
This function could be called with the array from question 6.18
as
int array[NROWS][NCOLUMNS];
int **array1; /* ragged */
int **array2; /* contiguous */
int *array3; /* "flattened" */
int (*array4)[NCOLUMNS];
The following two calls would probably work on most systems, but
involve questionable casts, and work only if the dynamic
ncolumns matches the static NCOLUMNS:
If you can understand why all of the above calls work and are
written as they are, and if you understand why the combinations
that are not listed would not work, then you have a *very* good
understanding of arrays and pointers in C.
6.21: Why doesn't sizeof properly report the size of an array when the
array is a parameter to a function?