Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initialization of variable length arrays

Reply
Thread Tools

Initialization of variable length arrays

 
 
jaime
Guest
Posts: n/a
 
      06-14-2007
Hi all.

The source code download bundle for "Beginning C: From Novice to
Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
contains a C source file (program9_09.c) which contains several instances
of the following type of idiom:


/* Program 9.9 REVERSI An Othello type game */
const int SIZE = 6;
int main(void)
{
char board [SIZE][SIZE] = { 0 };
return 0;
}


Now I'm a complete newbie with C (hence me reading this book), but I've had
a glance at "ISO/IEC 9899:TC2" and as far as I can tell:

1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
"variable length array", and

2) "6.7.8 Initialization Point 3" tells me that initializers do not
initialize variable length arrays.

Could the panel please tell me:

a) Am I on the right track here?
b) Is the above C illegal? Something very similar appears to
have been overlooked several times in the thread at:
http://groups.google.com.my/group/co...d4f798467dad19
c) Is the aforementioned book a known "lemon"? (Perhaps it's time for me
to try k&r2 again).

Thanks in advance to all. Jaime
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      06-14-2007
On Jun 14, 4:35 pm, jaime <(E-Mail Removed)> wrote:
> Hi all.
>
> The source code download bundle for "Beginning C: From Novice to
> Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
> contains a C source file (program9_09.c) which contains several instances
> of the following type of idiom:
>
> /* Program 9.9 REVERSI An Othello type game */
> const int SIZE = 6;
> int main(void)
> {
> char board [SIZE][SIZE] = { 0 };
> return 0;
>
> }
>
> Now I'm a complete newbie with C (hence me reading this book), but I've had
> a glance at "ISO/IEC 9899:TC2" and as far as I can tell:
>
> 1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
> "variable length array", and
>
> 2) "6.7.8 Initialization Point 3" tells me that initializers do not
> initialize variable length arrays.
>
> Could the panel please tell me:
>
> a) Am I on the right track here?
> b) Is the above C illegal? Something very similar appears to
> have been overlooked several times in the thread at:http://groups.google.com.my/group/co...ead/thread/246...
> c) Is the aforementioned book a known "lemon"? (Perhaps it's time for me
> to try k&r2 again).
>
> Thanks in advance to all. Jaime


You're right, it's broken. I think he used a C++ compiler, if he even
got it to compile.

$ gcc -Wall -ansi -pedantic -W -std=c99 broke.c
broke.c: In function 'main':
broke.c:5: error: variable-sized object may not be initialized
broke.c:5: warning: missing braces around initializer
broke.c:5: warning: (near initialization for 'board[0]')
broke.c:5: warning: excess elements in array initializer
broke.c:5: warning: (near initialization for 'board[0]')
broke.c:5: warning: excess elements in array initializer
broke.c:5: warning: (near initialization for 'board')
broke.c:5: warning: unused variable 'board'

$ g++ -W -Wall -ansi broke.c
broke.c: In function 'int main()':
broke.c:5: warning: unused variable 'board'

I have not read the book, but I guess it's a stinker.

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      06-14-2007
In article <f4sjc0$20fi$(E-Mail Removed)>,
jaime <(E-Mail Removed)> wrote:

>const int SIZE = 6;
>int main(void)
>{
> char board [SIZE][SIZE] = { 0 };
> return 0;
>}


>Now I'm a complete newbie with C (hence me reading this book), but I've had
>a glance at "ISO/IEC 9899:TC2" and as far as I can tell:
>
>1) "6.7.5.2 Array declarators Point 4" tells me that this must be a
>"variable length array", and
>
>2) "6.7.8 Initialization Point 3" tells me that initializers do not
>initialize variable length arrays.


You're right as far as I can see, and gcc agrees. It would of course
work with #define SIZE 6. The program also tries to pass an array of
int to a function expecting an array of bool.

I suspect the author attempted to update it to take advantage of C99,
but failed to try compiling it (a mistake when posting to Usenet, and
a Really Big mistake when publishing a book).

(I see it also fails to check the return value from scanf(), resulting
in stupid behaviour if type in, say, "d2" instead of "2d".)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2007
jaime wrote:
> Hi all.
>
> The source code download bundle for "Beginning C: From Novice to
> Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress)
> contains a C source file (program9_09.c) which contains several instances
> of the following type of idiom:
>
>
> /* Program 9.9 REVERSI An Othello type game */
> const int SIZE = 6;
> int main(void)
> {
> char board [SIZE][SIZE] = { 0 };
> return 0;
> }
>

This is legal C++, but not C99.

C++ made the sensible change to use 'const int' as a compile time
constant, so to a C++ compiler the array in question is not a VLA, but a
compile time fixed size array.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable-length arrays: should they be used at all? Rui Maciel C Programming 56 07-10-2012 06:12 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Variable length arrays Erwin Lindemann C Programming 45 03-08-2008 04:44 AM
Variable length arrays Q mechanicfem@googlemail.com C Programming 6 02-21-2006 04:32 AM
variable length arrays E. Robert Tisdale C Programming 21 10-13-2003 10:57 PM



Advertisments