#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    26
    Rep Power
    0

    C: constants, variable array length and C99


    Okay, I am more than a bit confused. Assuming I have a constant at the beginning of my code, such as:

    Code:
    const int MAXROLL  = 400;
    Why then:

    Code:
    static char joinedRolls[MAXROLL*1] = {0};  // compiles fine
    
    static char joinedRolls[MAXROLL] = {0};
    
    /* produces:
    main.c:77: variable-sized object may not be initialized
    main.c:77: warning: excess elements in array initializer
    main.c:77: warning: (near initialization for `joinedRolls')
    main.c:77: storage size of `joinedRolls' isn't constant
    main.c:77: size of variable `joinedRolls' is too large
    */
    What is the difference between MAXROLL*1 and MAXROLL, since mathematically they are equivalent? I know you can't have variable length arrays, but why does the above happen?

    In another part of my code I do this:

    Code:
      int dieNumberAsInt = atoi(dieNumber);
      int dieSidesAsInt  = atoi(dieSides);
      int rolls[dieNumberAsInt];
    And somebody on comp.lang.c commented that I must have a C99 compiler. To me it seems that this looks more like creating an array of variable length since dieNumber and dieSides can be different with each input (this is being looped through). Yet, this compiles just fine.

    Along the lines of "you must be doing C99", my loops look like this:

    Code:
        int i;
        for (i = 1; i <= dieNumberAsInt; i++) {
    It was suggested in comp.lang.c that:

    "If you're going to use C99 features such as variable length arrays and
    mixed-in declarations, you might as well be consistent and write

    for (int i = 1; i <= dieNumberAsInt; i++) {..."

    However, gcc complains:

    "`for' loop initial declaration used outside C99 mode"

    So, since I can't initialize an array with a const int (unless I multiply it by 1 for some reason), and I can create variable length arrays (as possibly illustrated by the atoi section above), AND since I can't write for loops in a sensible manner .. .. er, what the heck!? I don't even know what to ask here. I don't know what is going on. Am I mixing C99 and .. er, whatever else? Is that my problem?

    If this helps: gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    I thought gcc had supported variable-length arrays as a non-standard extension for some time, C99 or otherwise, is that not the case?

    Maybe check out the gcc documentation.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    26
    Rep Power
    0
    The program compiles just fine with g++ with either change to the initialization of joinedRolls. Not that explains anything, because I still don't understand the things pointed out, but I thought I'd mention it.
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    My point was that gcc has (I believe) accepted variable length arrays for some time (as a non-standard language extension). Not just for C99. So the fact that it accepts variable length arrays tells us nothing with respect to C99.

    That it then complains that you're declaring a variable within a for loop (which is only permitted in C99, not C89) suggests that you're compiling in C89 mode, with the permitted variable length array extension that gcc provides. It recognises that C99 permits such a declaration but isn't expecting to compile C99 code.

    That seems to me to be consistent with what you described.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    26
    Rep Power
    0
    Ah, my mistake entirely then with regards to the for loop.

    If gcc is allowing variable length arrays, why the problem with that MAXROLLS section? Why can I declare joinedRolls[MAXROLLS*1] but not joinedRolls[MAXROLLS] .. and the warning says that a variable sized object may not be initialized .. .. but I thought, as you said, that gcc -does- support variable sizes?

    I guess I'm just confusing all the different implementations (basic like C89 and C99, but also compiler specific).

    Thanks for the input.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0
    I think the answer to your question is that compiler does not support variable sized arrays...and a const variable is still a variable..actually a const variable can be changed using pointers....but either way the compiler still sees it as a variable. When you multiply it by one, it is casting it as a constant 'number'. As for the for loop, i think the other user was just trying to point out that you can initialize a variable within a loop with that compiler. Hope this answers your question.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    26
    Rep Power
    0
    Now if I can just figure out how to get it to initialize a variable within that for loop. I've not set up gcc any way besides default, so if it isn't doing it I certainly don't know why.

    I'm new to C (but not programming), so there's no doubt I'll run into more oddities such as this.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    You can initialize the variable in the for loop. You just can't define it there.

IMN logo majestic logo threadwatch logo seochat tools logo