Initialization of variable length arrays

J

jaime

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/comp.std.c/browse_thread/thread/2467e1e3a28a34a1/a5d4f798467dad19
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 :)
 
U

user923005

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/comp.std.c/browse_thread/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.
 
R

Richard Tobin

jaime said:
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
 
I

Ian Collins

jaime said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top