C Array declaration with const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olaf

    #1

    C Array declaration with const

    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
  • Richard Tobin

    #2
    Re: C Array declaration with const

    In article <fclfp4$2oh$1@f uerst.cs.uni-magdeburg.de>,
    Olaf <is.er@inter.ne twrote:
    >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
    --
    "Considerat ion shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

    Comment

    • Jack Klein

      #3
      Re: C Array declaration with const

      On Mon, 17 Sep 2007 11:01:26 +0200, Olaf <is.er@inter.ne twrote 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.l earn.c-c++

      Comment

      • Olaf

        #4
        Re: C Array declaration with const

        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

        Comment

        • Ben Pfaff

          #5
          Re: C Array declaration with const

          Olaf <is.er@inter.ne twrites:
          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
          "unwarrante d 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."

          Comment

          Working...