943,514 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1850
  • C RSS
Jan 2nd, 2009
0

variable length array may not be initialized: error

Expand Post »
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikileshsa is offline Offline
1 posts
since Jan 2009
Jan 2nd, 2009
0

Re: variable length array may not be initialized: error

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)
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 2nd, 2009
1

Re: variable length array may not be initialized: error

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 2nd, 2009
0

Re: variable length array may not be initialized: error

Click to Expand / Collapse  Quote originally posted by Narue ...
Wow, grumpier. That was total BS.
What can I say? I was tired. My sig also applies.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Query related to Hiding symbol in Shared Library
Next Thread in C Forum Timeline: changing color of a pushbutton on a dialog box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC