Initializer element is not constant

F

fred

Hi,

Can someone explain me why gcc-4.0 gives me the 'Initializer element is
not constant' error with this code ? Everything seems to be constant
here...

#include <stdio.h>
typedef struct { int a; int b;} t;
int main(int argc, char** argv)
{
static t* array[2] = {
(t[1]){ {1, 2} },
(t[2]){ {3, 4}, {5, 6} } };
return 0;
}

Thanks,
Fred
 
R

Robert Gamble

Hi,

Can someone explain me why gcc-4.0 gives me the 'Initializer element is
not constant' error with this code ? Everything seems to be constant
here...

#include <stdio.h>
typedef struct { int a; int b;} t;
int main(int argc, char** argv)
{
static t* array[2] = {
(t[1]){ {1, 2} },
(t[2]){ {3, 4}, {5, 6} } };
return 0;
}

A compound literal is not a constant.

Robert Gamble
 
W

whyglinux

Hi,

Can someone explain me why gcc-4.0 gives me the 'Initializer element is
not constant' error with this code ? Everything seems to be constant
here...

#include <stdio.h>
typedef struct { int a; int b;} t;
int main(int argc, char** argv)
{
static t* array[2] = {
(t[1]){ {1, 2} },
(t[2]){ {3, 4}, {5, 6} } };
return 0;
}

Static objects (such as `array` in the program), when initialized,
require their initializers to be constant expressions. The elements of
`array` array are of type t*, a pointer, so their initializers should
be address constants. However, compound literals in a function are not
static objects, and thus the addresses are not constants and can not be
taken as the initializers.
 
F

fred

Robert Gamble a écrit :
Hi,

Can someone explain me why gcc-4.0 gives me the 'Initializer element is
not constant' error with this code ? Everything seems to be constant
here...

#include <stdio.h>
typedef struct { int a; int b;} t;
int main(int argc, char** argv)
{
static t* array[2] = {
(t[1]){ {1, 2} },
(t[2]){ {3, 4}, {5, 6} } };
return 0;
}

A compound literal is not a constant.

Robert Gamble

OK, I see, but is it only a C restriction ? Because it seems that
everything can be known at compile time in this code so that the
compiler should be able to do something in this case.
 
F

fred

whyglinux a écrit :
Hi,

Can someone explain me why gcc-4.0 gives me the 'Initializer element is
not constant' error with this code ? Everything seems to be constant
here...

#include <stdio.h>
typedef struct { int a; int b;} t;
int main(int argc, char** argv)
{
static t* array[2] = {
(t[1]){ {1, 2} },
(t[2]){ {3, 4}, {5, 6} } };
return 0;
}

Static objects (such as `array` in the program), when initialized,
require their initializers to be constant expressions. The elements of
`array` array are of type t*, a pointer, so their initializers should
be address constants. However, compound literals in a function are not
static objects, and thus the addresses are not constants and can not be
taken as the initializers.

OK, I tried to initialize/declare 'array' in the global context, and it
compiles. I can't understand why those compound literals are not static
in a function. Can anyone tell me ?
 
W

whyglinux

OK, I tried to initialize/declare 'array' in the global context, and it
compiles. I can't understand why those compound literals are not static
in a function. Can anyone tell me ?

6.5.2.5 Compound literals, paragraph 6:

.... If the compound literal occurs outside the body of a function, the
object has static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top