C Array declaration with const

Discussion in 'C Programming' started by Olaf, Sep 17, 2007.

  1. Olaf

    Olaf Guest

    Hi,

    is there a way to declare an array with const variables like:

    static const ushort RX_BUF_SIZE = 0x100;

    typedef struct {
    volatile ushort buf[RX_BUF_SIZE];
    volatile unsigned head;
    volatile unsigned tail;
    volatile unsigned count;
    } RxBuffer;

    I've got the error: variable-size type declared outside of any function

    Thanks,
    Olaf
    Olaf, Sep 17, 2007
    #1
    1. Advertising

  2. In article <fclfp4$2oh$-magdeburg.de>,
    Olaf <> wrote:

    >is there a way to declare an array with const variables like:
    >
    >static const ushort RX_BUF_SIZE = 0x100;

    ....
    > volatile ushort buf[RX_BUF_SIZE];


    No. Use #define instead.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 17, 2007
    #2
    1. Advertising

  3. Olaf

    Jack Klein Guest

    On Mon, 17 Sep 2007 11:01:26 +0200, Olaf <> wrote in
    comp.lang.c:

    > Hi,
    >
    > is there a way to declare an array with const variables like:
    >
    > static const ushort RX_BUF_SIZE = 0x100;
    >
    > typedef struct {
    > volatile ushort buf[RX_BUF_SIZE];
    > volatile unsigned head;
    > volatile unsigned tail;
    > volatile unsigned count;
    > } RxBuffer;
    >
    > I've got the error: variable-size type declared outside of any function
    >
    > Thanks,
    > Olaf


    There is no way to make the value of any object a compile time
    constant, regardless of whether you use the const and/or the static
    keyword. C does not work that way, period, not at all.

    What you can do is:

    #define RX_BUF_SIZE 0x100

    ....or:

    enum { RX_BUF_SIZE = 0x100 };

    ....and then use the identifier RX_BUF_SIZE as a compile time constant
    for such purposes as defining the size of an array.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 18, 2007
    #3
  4. Olaf

    Olaf Guest

    Thanks for all answers!

    > What you can do is:
    >
    > #define RX_BUF_SIZE 0x100
    >
    > ...or:
    >
    > enum { RX_BUF_SIZE = 0x100 };
    >
    > ...and then use the identifier RX_BUF_SIZE as a compile time constant
    > for such purposes as defining the size of an array.


    Is this the so called enum-hack?

    Thanks
    Olaf
    Olaf, Sep 18, 2007
    #4
  5. Olaf

    Ben Pfaff Guest

    Olaf <> writes:

    > Is this the so called enum-hack?


    I don't think so. I've never heard of such a thing. Are you
    thinking of the "struct hack"? The C FAQ talks about it, but not
    under that name:

    2.6: I came across some code that declared a structure like this:

    struct name {
    int namelen;
    char namestr[1];
    };

    and then did some tricky allocation to make the namestr array
    act like it had several elements. Is this legal or portable?

    A: This technique is popular, although Dennis Ritchie has called it
    "unwarranted chumminess with the C implementation." An official
    interpretation has deemed that it is not strictly conforming
    with the C Standard, although it does seem to work under all
    known implementations. (Compilers which check array bounds
    carefully might issue warnings.)

    Another possibility is to declare the variable-size element very
    large, rather than very small; in the case of the above example:

    ...
    char namestr[MAXSIZE];

    where MAXSIZE is larger than any name which will be stored.
    However, it looks like this technique is disallowed by a strict
    interpretation of the Standard as well. Furthermore, either of
    these "chummy" structures must be used with care, since the
    programmer knows more about their size than the compiler does.
    (In particular, they can generally only be manipulated via
    pointers.)

    C9X will introduce the concept of a "flexible array member",
    which will allow the size of an array to be omitted if it is
    the last member in a structure, thus providing a well-defined
    solution.

    References: Rationale Sec. 3.5.4.2; C9X Sec. 6.5.2.1.

    --
    Peter Seebach on C99:
    "[F]or the most part, features were added, not removed. This sounds
    great until you try to carry a full-sized printout of the standard
    around for a day."
    Ben Pfaff, Sep 18, 2007
    #5
    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. Alexander Farber
    Replies:
    0
    Views:
    445
    Alexander Farber
    Jun 21, 2005
  2. Replies:
    11
    Views:
    1,087
  3. Javier
    Replies:
    2
    Views:
    543
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,090
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    871
    fungus
    Oct 31, 2008
Loading...

Share This Page