By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Initialization of variable length arrays

P: n/a
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 :-)
Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jun 14, 4:35 pm, jaime <nospamfo...@example.orgwrote:
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.

Jun 14 '07 #2

P: n/a
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.
Jun 15 '07 #3

P: n/a
In article <f4***********@energise.enta.net>,
jaime <no*********@example.orgwrote:
>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.
Jun 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.