Re: Array definition and extern declaration with different size.



Jack Klein wrote:
On 30 Jul 2006 20:48:10 -0700, "joshc" <josh.curtz@xxxxxxxxx> wrote in
comp.lang.c:

Hi,

I have an array defined in one file with an intializer as follows:

int arr[] = {0, 1, 2, 3};

I have a declaration of the array in another file as follows:

extern int arr[10];

This compiles without a problem on my implementation and arr ends up
being of size 10 on my implementation. Is this legal in "standard C"? I
was reading question 1.24 of the FAQ but that didn't seem to answer my
question in this case. Is the array definition with the initializer
somehow an incomplete definition?

Thanks for the help,

Josh

Any size specified in the external declaration is ignored by the
compiler. The array contains four ints. Any attempt to access past
arr [3] produces undefined behavior.

This is the same as using a size in an array style parameter
definition (an unfortunate choice anyway), with the exception of the
C99 feature where the "static" keyword is used.

That may be what happens for some compilers, but it is not what the
standard says, and if I recall correctly, there are implementations
that would display a warning or error message for such code during
linking.

n1124 6.2.7#2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

n1124 6.7.5.2#6:
"For two array types to be compatible, both shall have compatible
element types, and if both size specifiers are present, and are
integer constant expressions, then both size specifiers shall have the
same constant value. If the two array types are used in a context which
requires them to be compatible, it is undefined behavior if the two
size specifiers evaluate to unequal values."

.



Relevant Pages

  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)
  • RE: Design orientated C question
    ... You *must* include both times, each CPP file compiles independently, ... //Function declaration ... This code compiles OKAY if I include the following line of code in the ... when I am passing the array of structures by the calling ...
    (microsoft.public.vc.language)
  • Re: Difference between C and advanced C
    ... static and type qualifiers in parameter array declarators ... trailing comma allowed in enum declaration ... additional predefined macro names ... additional strftime conversion specifiers ...
    (comp.lang.c)
  • Re: avoiding writing an interface blocks
    ... the reason this needs an explicit interface is because of the ... example, an array, and not specify the argument types. ... type, period, with any kind of declaration. ... C-type pointers, including to ...
    (comp.lang.fortran)
  • Re: Does the order of declarations matter?
    ... > subroutine sub ... > Or must the declaration for N appear before the declaration ... parameter or array bound shall be specified in a prior ...
    (comp.lang.fortran)

Loading