Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C error: initializer element is not constant

Name: Anonymous 2009-02-04 15:43

I have some code that looks like this.


typedef struct {
   void *ptr;
} Foo;

static const size_t a = 1024;
static const size_t b = 1024;
static const size_t c = 1024;
static const size_t d = a * b + c * sizeof(Foo);


On the line declaring and initializing "d", I get the error "error: initializer element is not constant". Now, maybe I'm missing something, but aren't all of the operands there constant, therefore known at compile-time, therefore usable when initializing a constant?

Name: Anonymous 2009-02-04 15:45

>>1
You know, compilers aren't purrfect.

Name: Anonymous 2009-02-04 17:12

a, b and c are constant variables

Name: Anonymous 2009-02-04 17:16

What kind of compiler is that?
sizeof() is constant.

Name: Anonymous 2009-02-04 17:28

>>4
GCC.

This has been discussed on http://www.reddit.com/r/programming/comments/7uwr6/c_error_initializer_element_is_not_constant/

I don't agree with it, but there's no point in fighting with the compiler. At least I've found a workaround.

Name: Anonymous 2009-02-05 4:08

Just use #define and quit being a dumbass.

Name: Anonymous 2009-02-05 4:24

>>6
expert c hacker

Name: Anonymous 2009-02-05 4:34

>>5
GCC is pronounced 'Gucci'.

Don't change these.
Name: Email:
Entire Thread Thread List