Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initializer element is not constant

Reply
Thread Tools

Initializer element is not constant

 
 
fred@mayot.net
Guest
Posts: n/a
 
      08-08-2006
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

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      08-08-2006
wrote:
> 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

 
Reply With Quote
 
 
 
 
whyglinux
Guest
Posts: n/a
 
      08-08-2006
wrote:

> 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.

 
Reply With Quote
 
fred@mayot.net
Guest
Posts: n/a
 
      08-08-2006
Robert Gamble a écrit :

> wrote:
> > 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.

 
Reply With Quote
 
fred@mayot.net
Guest
Posts: n/a
 
      08-08-2006

whyglinux a écrit :

> wrote:
>
> > 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 ?

 
Reply With Quote
 
whyglinux
Guest
Posts: n/a
 
      08-08-2006
f...@mayot.net wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"initializer element is not constant" emin.martinian@gmail.com Python 1 05-30-2006 10:15 PM
playfunc.c:524: error: initializer element is not constant Levi Campbell C Programming 3 02-11-2006 10:34 PM
Initializer element not constant grid C Programming 8 08-02-2005 11:56 AM
initializer element is not constant Scott C Programming 15 01-24-2005 04:33 AM
Error: "initializer element is not constant" Todd Nathan C Programming 2 07-30-2003 04:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57