Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucsd!usc!samsung!munnari.oz.au!bruce!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: extern char *foo vs. extern char foo[] Summary: the compilers are right Message-ID: <3105@goanna.cs.rmit.oz.au> Date: 30 May 90 07:35:06 GMT References: <1990May30.001219.23564@uunet!unhd> Distribution: comp Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 40 In article <1990May30.001219.23564@uunet!unhd>, rg@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."