Prev: Accessor Functions (getter) for C String (Character Array) Members
Next: Accessor Functions (getter) for C String (Character Array) Members
From: aejeet on 23 Jun 2010 20:04 Please help me with this. Sample 1 compiles but Sample 2 throws error. I am not able to reason why SAMPLE 1 //const int i[] = {10,20,30,40,50}; //float f[i[2]]; int main() { const int i[] = {10,20,30,40,50}; float f[i[2]]; } SAMPLE 2 const int i[] = {10,20,30,40,50}; float f[i[2]]; int main() { // const int i[] = {10,20,30,40,50}; // float f[i[2]]; } ~ ~ ~ ~ -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 24 Jun 2010 09:39 On 24 Jun., 13:04, aejeet <aej...(a)gmail.com> wrote: > Please help me with this. Sample 1 compiles but Sample 2 throws error. Do you mean, that the compiler rejects the code as ill-formed? This would be a very different thing than "throws an error". > I am not able to reason why > > SAMPLE 1 > > //const int i[] = {10,20,30,40,50}; > //float f[i[2]]; > > int main() > { > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > } You are probably using a C compiler with support for variable length arrays (VLAs) as of C99, but this code is ill-formed in C++, including C++0x. > SAMPLE 2 > > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > > int main() > { > // const int i[] = {10,20,30,40,50}; > // float f[i[2]];} Again, this is ill-formed in C++, but this time it is also ill-formed for a C99 compiler as of 6.7.5.2: "If an identifier is declared to be an object with static storage duration, it shall not have a variable length array type." This is probably related to the fact that a program cannot change the size during runtime for such an array, so this would be no advantage (even function static locals must be initialized at the beginning of the program in C). HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Saeed Amrollahi on 24 Jun 2010 09:46 On Jun 24, 3:04 pm, aejeet <aej...(a)gmail.com> wrote: > Please help me with this. Sample 1 compiles but Sample 2 throws error. > I am not able to reason why > > SAMPLE 1 > > //const int i[] = {10,20,30,40,50}; > //float f[i[2]]; > > int main() > { > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > > } > > SAMPLE 2 > > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > > int main() > { > // const int i[] = {10,20,30,40,50}; > // float f[i[2]];} > > ~ > ~ > ~ > ~ { quoted banner removed; please do it yourself. -mod } No. It isn't true. For both samples, compiler issues expected constant expression error for f array bound. I compiled the code by Visual Studio 2008. Regards, -- Saeed Amrollahi -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Pavel Minaev on 24 Jun 2010 10:02
On Jun 24, 4:04 am, aejeet <aej...(a)gmail.com> wrote: > Please help me with this. Sample 1 compiles but Sample 2 throws error. > I am not able to reason why > > SAMPLE 1 > > //const int i[] = {10,20,30,40,50}; > //float f[i[2]]; > > int main() > { > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > > } > > SAMPLE 2 > > const int i[] = {10,20,30,40,50}; > float f[i[2]]; > > int main() > { > // const int i[] = {10,20,30,40,50}; > // float f[i[2]];} Neither sample is valid ISO C++. Apparently, your compiler implements C99 variable-length arrays (which in C++ context are a non-Standard language extension), which is why sample #1 works for you. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |