extern char *foo vs. extern char foo[]

Mark Bailey mark at isi.UUCP
Fri Jun 1 02:38:33 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 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.
> FILE #1:
> 
>      char hello[100];
> 
> FILE #2:
> 
>      extern char *hello;
> 


In one case, you are allocating an array of 100 characters.  The name of the
array refers to the base address of the array:  (K&R 1, p. 24.)

     When the name of an array is used as an argument, the value passed
     to the function is actually the location or address of the
     beginning of the array.

In the other case, you are passing the value of an unitialized character
pointer.  The result of looking at this is machine/compiler dependent.

It is the declarations:
     char     foo[];  and
     char     *foo;  

which are identical.  (K&R 1, p. 95)

     As formal parameters in a function definition,
               char    s[];
                                    and
               char   *s;
     are exactly equivalent;  which one should be written is determined
     largely by how expressions will be written in the function.

The reference manual appears to allow such a declaration which is not
used as a formal parameter (K&R 1, p. 194).

Mark Bailey                                 (I didn't really say this.)
via:  ...!uunet!pyrdc!isi!mark              ------Have a  8-|  day!!!!!
-- 
Mark Bailey                                 (I didn't really say this.)
via:  ...!uunet!pyrdc!isi!mark              ------Have a  8-|  day!!!!!



More information about the Comp.lang.c mailing list