C question
chris at umcp-cs.UUCP
chris at umcp-cs.UUCP
Thu Jul 17 13:57:44 AEST 1986
In article <306 at cord.UUCP> fjh at cord.UUCP (FJ Hirsch) writes:
>What is the difference between:
>
>extern char *A;
>
>and
>
>extern char A[];
Quite a bit. The former tells the compiler that there is a variable
named `A' defined externally, and that its type is `pointer to
char'. The latter tells it that there is a variable named `A'
defined externally, and that its type is `array of char'. These
are most certainly *not* equivalent. A Vax compiler, for example,
treats the former as a four-byte variable, and the latter as a
`link-time constant'.
>If you do: printf("A=%s\n",A);
>the first causes a core dump, the second works.
In that case, the actual defintion must have been
char A[<constant>];
>I thought pointers and arrays were equivalent?
No. This confusion seems to arise from C's convention of treating
the name of an array (in many places, but not all) as a pointer to
the first element of that array. It is heightened by C's convention
of allowing an `array' declaration as a formal parameter:
f(s)
char s[];
{
...
It might have been better if compilers refused to accept such a
declaration, rather than silently altering it to read
char *s;
So when *is* an object of type `array of <stuff>' treated as an
object of type `pointer to <stuff>'? If I have not forgotten
anything, the answer is `everywhere except in sizeof and in actual
declarations'. `extern char *A' and `extern char A[]' are actual
(not formal) declarations.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP: seismo!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list