rfg@paris.ics.uci.edu (Ronald Guilmette) (02/26/90)
I have several trivial questions about array declarations. I'm just trying to figure out what is considered legal based on sections 3.5, 3.5.4.2, and 3.5.7 of the December 1988 draft. In section 3.5.7 it says: "The type of the entity to be initialized shall be an object type or an array of unknown size." I do not understand what that last part of the sentence is trying to say. Does this mean that the following declaration is illegal? int array1[3] = { 1,2,3 }; /* size is known! */ How about the following pair of declarations? Are these legal or illegal? extern int array2[3]; int array2[] = { 1,2,3 }; /* size is known! */ Now assuming that the above pair of declarations is legal, how about the following pairs? Are these legal? extern int array3[4]; /* size = 4 words */ int array3[] = { 1,2,3 }; /* size = 3 words! */ extern int array4[3]; /* size = 3 words */ extern array4[] = { 1,2,3,4 }; /* size = 4 words */ In the above example, the array sizes are apparently in conflict, but those are cases where the conflict involves an array size which is derived implicitly from an initializer. How about the following examples, in which the array size conflicts do not involve array sizes derived implicitly from initializers? Are these legal or illegal? extern int array5[4]; int array5[3]; extern int array6[3]; int array6[4]; Finally, is there any legal way to "forward declare" a static-linkage array? For example, are any of the following pairs of declarations legal? static int array7[]; static int array7[3] = { 1,2,3 }; static int array8[3]; static int array8[3] = { 1,2,3 }; static int array9[3]; static int array9[] = { 1,2,3 }; static int array10[]; static int array10[] = { 1,2,3 }; // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.
gwyn@smoke.BRL.MIL (Doug Gwyn) (02/27/90)
In article <25E833AA.15362@paris.ics.uci.edu> rfg@paris.ics.uci.edu (Ronald Guilmette) writes:
-Does this mean that the following declaration is illegal?
- int array1[3] = { 1,2,3 }; /* size is known! */
No, array1 has object type.
-How about the following pair of declarations? Are these legal or illegal?
- extern int array2[3];
- int array2[] = { 1,2,3 }; /* size is known! */
array2 has object type.
-Now assuming that the above pair of declarations is legal, how about the
-following pairs? Are these legal?
- extern int array3[4]; /* size = 4 words */
- int array3[] = { 1,2,3 }; /* size = 3 words! */
array3 has object type. Because you omitted the fourth initializer,
array3[3] starts off containing a 0 value.
- extern int array4[3]; /* size = 3 words */
- extern array4[] = { 1,2,3,4 }; /* size = 4 words */
array 4 has object type. You have specified too many initializers
and should get a diagnostic.
- extern int array5[4];
- int array5[3];
These types are not compatible; error.
- extern int array6[3];
- int array6[4];
Ditto.
- static int array7[];
- static int array7[3] = { 1,2,3 };
- static int array8[3];
- static int array8[3] = { 1,2,3 };
- static int array9[3];
- static int array9[] = { 1,2,3 };
- static int array10[];
- static int array10[] = { 1,2,3 };
All these are okay.
blodgett@granite.cr.bull.com (Bruce Blodgett) (02/28/90)
In article <12235@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) replies: >- static int array7[]; >- static int array7[3] = { 1,2,3 }; >- static int array8[3]; >- static int array8[3] = { 1,2,3 }; >- static int array9[3]; >- static int array9[] = { 1,2,3 }; >- static int array10[]; >- static int array10[] = { 1,2,3 }; > >All these are okay. According to X3.159-1989 (the C Standard) section 3.7.2, "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." The first declarations of array7 and array10 are tentative, have internal linkage, and are incomplete. Therefore they are not allowed. Bruce Blodgett
karl@haddock.ima.isc.com (Karl Heuer) (02/28/90)
In article <12235@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >- static int array7[]; static int array7[3] = { 1,2,3 }; >- static int array8[3]; static int array8[3] = { 1,2,3 }; >- static int array9[3]; static int array9[] = { 1,2,3 }; >- static int array10[]; static int array10[] = { 1,2,3 }; > >All these are okay. That's what I thought, too, but when Ron said (in email) that gcc didn't like them all, I checked the ANS. 3.7.2 says "if the declaration... is a tentative definition and has internal linkage, the declared type shall not be an incomplete type". This seems to make array7 and array10 illegal. It's also a pretty stupid rule, in my opinion. Karl W. Z. Heuer (karl@ima.isc.com or harvard!ima!karl), The Walking Lint
gwyn@smoke.BRL.MIL (Doug Gwyn) (02/28/90)
In article <16045@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes: >... 3.7.2 says "if the declaration... is a tentative >definition and has internal linkage, the declared type shall not be an >incomplete type". This seems to make array7 and array10 illegal. Ok, I stand corrected. I'd forgotten that we had agreed to that. Tentative definitions were a real mess. >It's also a pretty stupid rule, in my opinion. I hope before everyone else in X3J11 forgets why the rules ended up the way they did, somebody documents it. The Rationale doesn't appear to cover this.
blodgett@granite.cr.bull.com (Bruce Blodgett) (03/01/90)
To determine the reason that the declaration static int array7[]; is not allowed, ask yourself, "if this is the only declaration of array7, how many elements should the array have?" The answer, of course, is that you can't tell. That is why this is not allowed. Bruce Blodgett
karl@haddock.ima.isc.com (Karl Heuer) (03/02/90)
In article <1990Feb28.221846.25750@granite.cr.bull.com> blodgett@granite.cr.bull.com (Bruce Blodgett) writes: >To determine the reason that the declaration > static int array7[]; >is not allowed, ask yourself, "if this is the only declaration of >array7, how many elements should the array have?" The answer, of >course, is that you can't tell. That is why this is not allowed. So why doesn't the language wait to see if it *is* the only declaration before rejecting it? In other words, why isn't that a legal non-defining forward declaration? Note that "int foo[];" is legal, though it has the same problem. Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint