[Newbie] Const and Array

Discussion in 'C Programming' started by Cadderly, Aug 17, 2003.

  1. Cadderly

    Cadderly Guest

    Hello,

    I wonder why my compiler (Bcc55) complains about this one saying:
    Error E2313 temp.c 6: Constant expression required in function main

    int main(void)
    {
    const int BUF = 100;
    char bufarr[BUF];

    return 0;
    }
    Cadderly, Aug 17, 2003
    #1
    1. Advertising

  2. Cadderly

    Matt Gregory Guest

    Cadderly wrote:
    > Hello,
    >
    > I wonder why my compiler (Bcc55) complains about this one saying:
    > Error E2313 temp.c 6: Constant expression required in function main
    >
    > int main(void)
    > {
    > const int BUF = 100;
    > char bufarr[BUF];
    >
    > return 0;
    > }


    Basically, BUF is assigned at run time, and the compiler needs to know
    the size of bufarr at compile time. You have to use a #define or an
    enumeration for array indexes.
    Matt Gregory, Aug 17, 2003
    #2
    1. Advertising

  3. Cadderly

    Mark Gordon Guest

    On Sun, 17 Aug 2003 22:59:25 +0200
    "Cadderly" <> wrote:

    > Hello,
    >
    > I wonder why my compiler (Bcc55) complains about this one saying:
    > Error E2313 temp.c 6: Constant expression required in function main
    >
    > int main(void)
    > {
    > const int BUF = 100;
    > char bufarr[BUF];
    >
    > return 0;
    > }


    It's quite simple. Despite const being an abbreviation of constant it
    does not really declare constants in C. If you use
    enum { BUF=100 };
    then you will actually have a constant which follows the expected
    scoping rules, alternatively use a #define.
    --
    Mark Gordon
    Mark Gordon, Aug 17, 2003
    #3
  4. Cadderly

    Cadderly Guest

    > Basically, BUF is assigned at run time, and the compiler needs to know
    > the size of bufarr at compile time. You have to use a #define or an
    > enumeration for array indexes.
    >


    Thanks, I know that the compiler needs to know the size of the array at
    compile time but why BUF is assigned at run time? Since BUF is a constant
    what's the point?
    Cadderly, Aug 17, 2003
    #4
  5. "Cadderly" <> wrote in
    <bhortp$1lgsb$-berlin.de>:

    >Thanks, I know that the compiler needs to know the size of the array at
    >compile time but why BUF is assigned at run time? Since BUF is a constant
    >what's the point?
    >

    From your point of view

    const int BUF = 100;

    defines an integer constant. From C's (and therewith your
    compiler's) POV, BUF is an integer variable whos value cannot
    be changed. And variables (const or not) are evaluated at run
    time. Looks strange to beginners, but that's the way C deals
    with it...

    Irrwahn

    --
    If you don't care where you are, then you ain't lost.
    Irrwahn Grausewitz, Aug 17, 2003
    #5
  6. Cadderly

    Matt Gregory Guest

    Cadderly wrote:

    >>Basically, BUF is assigned at run time, and the compiler needs to know
    >>the size of bufarr at compile time. You have to use a #define or an
    >>enumeration for array indexes.
    >>

    >
    >
    > Thanks, I know that the compiler needs to know the size of the array at
    > compile time but why BUF is assigned at run time? Since BUF is a constant
    > what's the point?


    Well, you can use a function to initialize it. I think the point
    is to keep the language simple by not treating literals as exceptions
    to the rule.
    Matt Gregory, Aug 17, 2003
    #6
  7. Cadderly

    Cadderly Guest

    compile time but why BUF is assigned at run time? Since BUF is a constant
    > what's the point?
    >
    >


    well, thank you both..
    Cadderly, Aug 17, 2003
    #7
  8. "Cadderly" <> writes:

    > int main(void)
    > {
    > const int BUF = 100;
    > char bufarr[BUF];
    >
    > return 0;
    > }

    [...]
    > Since BUF is a constant what's the point?


    `BUF' is not a constant, `BUF' is a const-qualified variable.
    IOW, a variable which cannot be modified, but still a variable.

    Martin
    Martin Dickopp, Aug 17, 2003
    #8
  9. Cadderly

    Matt Gregory Guest

    I wrote:

    > Well, you can use a function to initialize it. I think the point
    > is to keep the language simple by not treating literals as exceptions
    > to the rule.


    Plus there's the point that since you declared BUF inside main(),
    that piece of memory doesn't exist until execution enters main().
    Matt Gregory, Aug 17, 2003
    #9
  10. Cadderly

    Al Bowers Guest

    Cadderly wrote:
    >>Basically, BUF is assigned at run time, and the compiler needs to know
    >>the size of bufarr at compile time. You have to use a #define or an
    >>enumeration for array indexes.
    >>

    >
    >
    > Thanks, I know that the compiler needs to know the size of the array at
    > compile time but why BUF is assigned at run time? Since BUF is a constant
    > what's the point?
    >
    >


    C language now defines the variable length array (vla) type. You
    will have to upgrade your compiler to a version that supports the
    current C Standard, or at least supports vla's. For example, the
    Standard now supports the following vla example:

    #include <stdio.h>

    int main(void)
    {
    int n = 5,i;
    int array[n];

    for(i = 0; i < 5;i ++)
    {
    array = i;
    printf("array[%d] = %d\n",i,array);
    }
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
    Al Bowers, Aug 18, 2003
    #10
    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. Replies:
    11
    Views:
    1,082
  2. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,079
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    863
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    518
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page