| | |
variable length array may not be initialized: error
![]() |
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#include<stdio.h> # define A 10 const short LINE_SIZE = 255; main(){ char a[LINE_SIZE + 1] = " "; //Error here...why is this an error?? char b[A]=""; //no problem here printf("%d",sizeof(a)); printf("%s",a); }
Last edited by Narue; Jan 2nd, 2009 at 9:17 am. Reason: added code tags and formatting
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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)
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)
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.
>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:
>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.
>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:
c Syntax (Toggle Plain Text)
#include <string.h> int main ( void ) { int x = 10; int a[x]; strcpy ( a, " " ); /* ... */ return 0; }
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.
![]() |
Similar Threads
- Selection Sort in java (Java)
- Declaring multidimensional array in VBA (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Query related to Hiding symbol in Shared Library
- Next Thread: changing color of a pushbutton on a dialog box
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






