extern char *foo vs. extern char foo[]

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Wed May 30 17:35:06 AEST 1990


In article <1990May30.001219.23564 at uunet!unhd>, rg at uunet!unhd (Roger Gonzalez ) writes:
> According to K&R, there should be no difference between
  [	extern char *foo;
    and	extern char foo[];
  ]
> In fact, the second form (char foo[]) should get translated into the first.

No, it shouldn't, and it hasn't done since UNIX V6 at least.

Given	extern char *foo;
foo is a variable of type pointer-to-char with external linkage.
Foo's type is complete, (sizeof foo) is allowed.  You can assign to foo:
	foo = NULL;
and it is possible that
	(foo == NULL)
might be true.

Given	extern char baz[];
baz is a variable of type array-UNKNOWN-of-char with external linkage.
This is an "incomplete" type, you can't take (sizeof baz).
You cannot assign to foo, and although "foo" will decay into a pointer
in most contexts, it is not possible for
	(baz == NULL)
ever to be true.

Now, an 'extern' declaration is supposed to match up with one definition.
	extern char *foo;	matches		char *foo;
				DOES NOT MATCH	char foo[10];

	extern char baz[];	matches		char baz[10];
				DOES NOT MATCH	char *baz;

> This is the same output I got on 3 different machines, running Unisoft
> and Green Hills compilers.  So.  Why are my compilers so stupid?

Because they got it right.  The two forms of extern declaration are not
and were not equivalent.
-- 
"A 7th class of programs, correct in every way, is believed to exist by a
few computer scientists.  However, no example could be found to include here."



More information about the Comp.lang.c mailing list