Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP Newsgroups: net.lang.c Subject: Re: C question Message-ID: <2451@umcp-cs.UUCP> Date: Wed, 16-Jul-86 23:57:44 EDT Article-I.D.: umcp-cs.2451 Posted: Wed Jul 16 23:57:44 1986 Date-Received: Thu, 17-Jul-86 05:55:08 EDT References: <306@cord.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 50 In article <306@cord.UUCP> fjh@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[]; >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 ' treated as an object of type `pointer to '? 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@umcp-cs ARPA: chris@mimsy.umd.edu