Hi! i have use p89c669 microcontroller and am29f040b [512 kb] flash.When i using the external code memory i can't initialize the array that have more than 4 values.if i initialize more than that the program will not be executed.if i comment the array the program was executed.the code was
const char red [256] = {0x00,0x00...0x00,0x00};
please help me!
regrads, K.T.Venkatesan.
You can't have an initialiser on an extern declaration, can you?
Yes. Well, yes on a "definition" but not on a "declaration", to be pedantic, but the syntax is legal, and from the code snippets above you can't really tell whether it was meant to be a declaration. The initializer makes it a definition, and you'll get some sort of "multiply defined" diagnostic message if the syntax shows up in a .h file included in several .c files (for example).
From the ANSI C89 spec:
3.7.2 External object definitions Semantics If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier. A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static , constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0. If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type. Examples int i1 = 1; /* definition, external linkage */ static int i2 = 2; /* definition, internal linkage */ extern int i3 = 3; /* definition, external linkage */ int i4; /* tentative definition, external linkage */ static int i5; /* tentative definition, internal linkage */ int i1; /* valid tentative definition, refers to previous */ int i2; /* $3.1.2.2 renders undefined, linkage disagreement */ int i3; /* valid tentative definition, refers to previous */ int i4; /* valid tentative definition, refers to previous */ int i5; /* $3.1.2.2 renders undefined, linkage disagreement */ extern int i1; /* refers to previous, whose linkage is external */ extern int i2; /* refers to previous, whose linkage is internal */ extern int i3; /* refers to previous, whose linkage is external */ extern int i4; /* refers to previous, whose linkage is external */ extern int i5; /* refers to previous, whose linkage is internal */
Since I'm not a fan of programming languages doing much (if anything) implicitly, I prefer always to repeat the extern/static on the definition as well as the declarations. This at least earns me a diagnostic message when an object changes from external to internal, and serves as a useful reminder that "this object is accessible from outside", and keeps the syntax parallel with the "static" case.