Initialization of variable length arrays

Discussion in 'C Programming' started by jaime, Jun 15, 2007.

  1. jaime

    jaime Guest

    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 :)
    jaime, Jun 15, 2007
    #1
    1. Advertising

  2. jaime

    user923005 Guest

    On Jun 14, 4:35 pm, 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;
    >
    > }
    >
    > 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.
    user923005, Jun 15, 2007
    #2
    1. Advertising

  3. In article <f4sjc0$20fi$>,
    jaime <> 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.
    Richard Tobin, Jun 15, 2007
    #3
  4. jaime

    Ian Collins Guest

    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.
    Ian Collins, Jun 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E. Robert Tisdale

    variable length arrays

    E. Robert Tisdale, Sep 27, 2003, in forum: C Programming
    Replies:
    21
    Views:
    743
    Mark McIntyre
    Oct 13, 2003
  2. Variable length arrays Q

    , Feb 20, 2006, in forum: C Programming
    Replies:
    6
    Views:
    368
    Jack Klein
    Feb 21, 2006
  3. Erwin Lindemann

    Variable length arrays

    Erwin Lindemann, Mar 6, 2008, in forum: C Programming
    Replies:
    45
    Views:
    1,112
    Keith Thompson
    Mar 8, 2008
  4. Philipp
    Replies:
    21
    Views:
    1,105
    Philipp
    Jan 20, 2009
  5. Rui Maciel
    Replies:
    56
    Views:
    2,490
    Tim Rentsch
    Jul 10, 2012
Loading...

Share This Page