variable length array may not be initialized: error

Reply

Join Date: Jan 2009
Posts: 1
Reputation: nikileshsa is an unknown quantity at this point 
Solved Threads: 0
nikileshsa nikileshsa is offline Offline
Newbie Poster

variable length array may not be initialized: error

 
0
  #1
Jan 2nd, 2009
  1. #include<stdio.h>
  2. # define A 10
  3.  
  4. const short LINE_SIZE = 255;
  5.  
  6. main(){
  7. char a[LINE_SIZE + 1] = " "; //Error here...why is this an error??
  8. char b[A]=""; //no problem here
  9.  
  10. printf("%d",sizeof(a));
  11. printf("%s",a);
  12. }
Last edited by Narue; Jan 2nd, 2009 at 9:17 am. Reason: added code tags and formatting
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: variable length array may not be initialized: error

 
0
  #2
Jan 2nd, 2009
The first line puts a space into the first char of a, and a zero into the second char. All other elements of a are uninitialised. Hence the compiler diagnostic (which will usually be a warning, not an error).

The second line initialises the array b with a zero byte (that's what "" yields in memory). If one element of an array is initialised to zero, all elements are (i.e. the whole array is initialised)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: variable length array may not be initialized: error

 
0
  #3
Jan 2nd, 2009
Wow, grumpier. That was total BS.

>The first line puts a space into the first char of a, and a zero into
>the second char. All other elements of a are uninitialised.
String literal initialization is equivalent to a brace enclosed initialization list. char a[LINE_SIZE + 1] = " "; is essentially syntactic sugar for char a[LINE_SIZE + 1] = {' ', '\0'}; , so assuming the initialization is allowed (which it isn't, hence the error), all elements will be initialized in a predictable manner.

>If one element of an array is initialised to zero, all
>elements are (i.e. the whole array is initialised)
Rather, if there are fewer initializers than elements in the array, all uninitialized elements are implicitly initialized to the equivalent of {0} for that type. The explicit initializer doesn't have to be "zero", whatever you meant by that.

>char a[LINE_SIZE + 1] = " "; //Error here...why is this an error??
It's an error because const in C doesn't denote a compile-time constant. Therefore a is a variable length array (in C99). The C99 standard explicitly disallows initialization of variable length arrays. Because this is a constraint violation, an error is required. You fix it by doing an assignment rather than an initialization:
  1. #include <string.h>
  2.  
  3. int main ( void )
  4. {
  5. int x = 10;
  6. int a[x];
  7.  
  8. strcpy ( a, " " );
  9.  
  10. /* ... */
  11.  
  12. return 0;
  13. }
>char b[A]=""; //no problem here
A is a compile-time constant because it's replaced with 10 by the preprocessor. This means that b is a regular array and initialization is supported.

>main(){
Note that this is also a constraint violation in C99, so your compiler isn't strictly conforming to either C89 (where implicit int is allowed but VLAs aren't) or C99 (where VLAs are allowed but not implicit int) with the switches you're giving it. Presumably you're relying on C89 and a compiler's variable length array extension, which means the details of my explanation may be different, but the underlying problem appears to be the same.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: variable length array may not be initialized: error

 
0
  #4
Jan 2nd, 2009
Originally Posted by Narue View Post
Wow, grumpier. That was total BS.
What can I say? I was tired. My sig also applies.
Right 98% of the time, and don't care about the other 3%.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC