extern char *foo vs. extern char foo[]
Roger Gonzalez
rg at uunet!unhd
Wed May 30 10:12:19 AEST 1990
A little history: I know C inside and out, and don't need any "I think"s.
I have been aware of this "feature" for quite a while, and know how to
program around it. I am posting this to find out *why* things are done this
way, since it seems to violate what K&R say.
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. Unfortunately, it doesn't. Here's a
more detailed example of what I mean:
FILE #1:
char hello[100];
main()
{
strcpy(hello, "Hello, world.\n");
printf("In main(), hello is at %06X\n", hello);
foo();
}
FILE #2:
extern char *hello;
foo()
{
printf("In foo(), hello is at %06X\n", hello);
}
Now, theoretically, since C is not strongly typed, the two values printed
should be the same. They aren't. Try it if you don't believe me. If
you change file #2 so that the extern declaration is
extern hello[];
it works. It also will work properly if in file #1, hello is declared
to be a pointer to char, and is malloc-ated to 100 bytes. Well, I poked
around a little further; I stopped the output at the assembler stage, and
looked at the .s files for file #2 (using the first method of correction,
namely declaring extern char hello[]), did a diff, and guess what I found?
>
mov hello, (sp%) I know, I know. This isn't *quite* what I
< found, but I'm typing it from memory, and its
mov &hello, (sp%) close enough for government work.
Aha! Zees eez very wrong!
This is the same output I got on 3 different machines, running Unisoft
and Green Hills compilers. So. Why are my compilers so stupid? Do others
behave the same way, or am I just unfortunate enough to have lousy software?
If this is more widespread, WHY? Is this a manifestation of some other
C rule that takes precedence over what the "proper" behavior? Please clue
me in. Please.
-Roger
--
UUCP: ..!uunet!unhd!rg | USPS: Marine Systems Engineering Laboratory
BITNET: r_gonzalez at unhh | University of New Hampshire
PHONE: (603) 862-4600 | Marine Programs Building
FAX: (603) 862-4399 | Durham, NH 03824-3525
More information about the Comp.lang.c
mailing list