extern char *foo vs. extern char foo[]
Stephen Clamage
steve at taumet.COM
Thu May 31 02:51:22 AEST 1990
In article <1990May30.001219.23564 at uunet!unhd> rg at unhd.unh.edu.UUCP (Roger Gonzalez ) writes:
>
>A little history: I know C inside and out, and don't need any "I think"s.
Evidently not.
>According to K&R, there should be no difference between the two extern
>defs in my subject header. In fact, the second form (char foo[]) should
>get translated into the first.
Not true.
> char hello[100];
> extern hello[];
In both declarations, hello means the address of an array.
hello is a (link-time) constant and may not be assigned to.
> extern char *hello;
hello means the address of a variable which *contains* the address of an
array (or of a variable, which would be an array of size 1).
hello may be assigned to, so as to point to another array.
>So. Why are my compilers so stupid? Do others
>behave the same way, or am I just unfortunate enough to have lousy software?
Other compilers better behave the same way, since the language definition
requires it.
It is a common misunderstanding that arrays and pointers are in some sense
"the same thing". This is one case where they are not the same thing. The
difference is discussed in Chapter 5 of old original K&R, and this difference
has not changed. It is still a difference in ANSI C.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list