extern declaration inconsistency

jas at drutx.UUCP jas at drutx.UUCP
Thu Apr 5 03:46:16 AEST 1984


To paraphrase the question:

     Defining a global array "char ch[ 32 ];" in one file and
     declaring it externally as "extern char *ch" in another
     file causes bad craziness ("core dump").  Is this a bug
     or a feature?

It is most emphatically a feature.  An array of characters is not
the same thing as a character pointer.  Lying to the compiler about
the type of an external variable will result in severe retribution.

To wit:  a reference to ch in the file in which it was defined as
an array of 32 chars will be automatically dereferenced by the compiler,
i.e., converted to the address of the first element of the array, because 
arrays are automatically dereferenced when they appear in an expression.
A reference to ch in a file in which it was declared as "extern char *"
will cause the compiler to issue code retrieving the POINTER VALUE STORED
AT THAT LOCATION, i.e., to take the first several chars in the array
("several" usually = 2 or 4, depending on the machine), and interpret
them as a pointer to a character somewhere.  Interpreting the CONTENTS
of "Hi, Mom!" as a character pointer will usually make you point at 
something you later wish you hadn't pointed at.

Jim Shankland
..!ihnp4!druxy!jas



More information about the Comp.lang.c mailing list