C Array declaration with const

O

Olaf

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
 
R

Richard Tobin

Olaf said:
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
 
J

Jack Klein

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
 
O

Olaf

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
 
B

Ben Pfaff

Olaf said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top