Compiler error with struct and const or #define

Discussion in 'C Programming' started by jon.harding@gmx.co.uk, May 2, 2012.

  1. Guest

    Hello, I am learning C so excuse me if this is a trivial question.

    Here is some contrived code. It won't even compile with gcc. I need to
    set a size of the int array at compile time. When I provide copies of
    my code for evaluation, I want to set the dynamic size low. When I
    provide copies of my code when someone buys it, I want to set the
    dynamic size high (and recompile) and provide something more useful.
    The line with baa1 is fine and when it is the only array in my struct,
    it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
    Can someone advise of the syntax to dynamically size an array in a
    struct at compile time with gcc/Ubuntu x86.
    Thank you
    Jon

    #define TESTDYNAMICSIZE1 = 5
    const testdynamicsize2 = 5;

    struct foo1 {
    int id;
    int baa1[5];
    int baa2[TESTDYNAMICSIZE1];
    int baa3[testdynamicsize2];
    };

    int main(int argc, char *argv[]) {
    return 0;
    }
    , May 2, 2012
    #1
    1. Advertising

  2. John Gordon Guest

    In <> writes:

    > #define TESTDYNAMICSIZE1 = 5


    You don't need an equal sign here.

    > const testdynamicsize2 = 5;


    > struct foo1 {
    > int baa3[testdynamicsize2];
    > };


    This would work if it were inside a function, but it isn't allowed in
    global scope.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 2, 2012
    #2
    1. Advertising

  3. writes:

    > Hello, I am learning C so excuse me if this is a trivial question.
    >
    > Here is some contrived code. It won't even compile with gcc. I need to
    > set a size of the int array at compile time. When I provide copies of
    > my code for evaluation, I want to set the dynamic size low. When I
    > provide copies of my code when someone buys it, I want to set the
    > dynamic size high (and recompile) and provide something more useful.
    > The line with baa1 is fine and when it is the only array in my struct,
    > it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
    > Can someone advise of the syntax to dynamically size an array in a
    > struct at compile time with gcc/Ubuntu x86.


    You've made only a very small mistake, but I'd alter your terminology.
    It's not usual to refer to a compile-time size as "dynamic" even though
    you may change it it from time to time. The phrase "dynamic size" is
    more often used to describe thing (particularly arrays) whose
    size changes during run-time.

    > #define TESTDYNAMICSIZE1 = 5


    Drop the "=":

    #define TESTDYNAMICSIZE1 5

    > const testdynamicsize2 = 5;


    This one won't work. const objects in C are, rather confusingly, not
    guaranteed to be compile-time constants.

    > struct foo1 {
    > int id;
    > int baa1[5];
    > int baa2[TESTDYNAMICSIZE1];
    > int baa3[testdynamicsize2];
    > };


    <snip>
    --
    Ben.
    Ben Bacarisse, May 3, 2012
    #3
  4. Guest

    On May 3, 1:00 am, Ben Bacarisse <> wrote:
    > writes:
    > > Hello, I am learning C so excuse me if this is a trivial question.

    >
    > > Here is some contrived code. It won't even compile with gcc. I need to
    > > set a size of the int array at compile time. When I provide copies of
    > > my code for evaluation, I want to set the dynamic size low. When I
    > > provide copies of my code when someone buys it, I want to set the
    > > dynamic size high (and recompile) and provide something more useful.
    > > The line with baa1 is fine and when it is the only array in my struct,
    > > it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
    > > Can someone advise of the syntax to dynamically size an array in a
    > > struct at compile time with gcc/Ubuntu x86.

    >
    > You've made only a very small mistake, but I'd alter your terminology.
    > It's not usual to refer to a compile-time size as "dynamic" even though
    > you may change it it from time to time.  The phrase "dynamic size" is
    > more often used to describe thing (particularly arrays) whose
    > size changes during run-time.
    >
    > > #define TESTDYNAMICSIZE1 = 5

    >
    > Drop the "=":
    >
    > #define TESTDYNAMICSIZE1 5
    >
    > > const testdynamicsize2 = 5;

    >
    > This one won't work.  const objects in C are, rather confusingly, not
    > guaranteed to be compile-time constants.
    >
    > > struct foo1 {
    > >               int id;
    > >               int baa1[5];
    > >               int baa2[TESTDYNAMICSIZE1];
    > >               int baa3[testdynamicsize2];
    > > };

    >
    > <snip>
    > --
    > Ben.


    John and Ben, thank you. comp.lang.c is a very helpful and friendly
    group.
    Jon
    , May 3, 2012
    #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. Daniel Rudy
    Replies:
    7
    Views:
    447
    Daniel Rudy
    Mar 31, 2006
  2. Replies:
    4
    Views:
    409
    Alf P. Steinbach
    May 5, 2006
  3. ThazKool
    Replies:
    1
    Views:
    444
  4. Replies:
    14
    Views:
    692
    Kai-Uwe Bux
    Sep 18, 2006
  5. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
Loading...

Share This Page