473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

initializer element is not constant

On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initializer element is not constant" on the below
code. The code compiles fine with other compilers I
have. Is this valid C89 code?

extern int *b;

int *a = b;

int main(void)
{
return (0);
}

Thanks. Paul.
Oct 24 '06 #1
7 10154
Sorry folks, the code fails on my other compilers too.

It was this code:

#include <stdio.h>

FILE *a = stderr;

int main(void)
{
return (0);
}

that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.

BFN. Paul.
"Paul Edwards" <ke******@nosppaam.w3.towrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au...
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initializer element is not constant" on the below
code. The code compiles fine with other compilers I
have. Is this valid C89 code?

extern int *b;

int *a = b;

int main(void)
{
return (0);
}

Thanks. Paul.


Oct 24 '06 #2
On Tue, 24 Oct 2006 14:24:13 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initializer element is not constant" on the below
code. The code compiles fine with other compilers I
have. Is this valid C89 code?
No, it is not. Initializers for objects with static storage duration,
and that includes all objects defined at file scope, must be constant
expressions. You are trying to initialize the pointer 'a' with the
value of another object. The value of any object, even if it is a
const qualified object, is not a constant expression in C.
extern int *b;

int *a = b;

int main(void)
{
return (0);
}
Even this is not (necessarily) valid C:

int x;
int *const b = &x; /* valid, &x is an address constant */
int *a = b; /* not valid */

The initialization of 'a' is not valid even though the value of 'b' is
known at compile time, which it is not in your case. The value of an
object is just not among the defined constant expressions in the C
standard.

It is actually possible for a compiler to accept this as an extension,
that being the reason for the (necessarily) I wrote above, because the
C standard allows an implementation to "accept other forms of constant
expressions". But I don't know of any implementation that does.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 24 '06 #3
On Tue, 24 Oct 2006 14:29:37 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
Sorry folks, the code fails on my other compilers too.

It was this code:

#include <stdio.h>

FILE *a = stderr;

int main(void)
{
return (0);
}

that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.
This one is a different story. It works on some compilers and fails
on others. It depends on the compiler's definition of the macro
"stderr". The C standard requires that stdin, stdout, and stderr are
macros that expand to pointers to file, and depending on how the
macros are defined, they may or may not be address constants at
compile time.

So you should not use this, because as you found out it may work on
some implementations but not others.

The easy fix is to leave the pointer uninitialized, which means it
gets default initialized to NULL, and in main(), before it is used,
assign it.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 24 '06 #4
"Jack Klein" <ja*******@spamcop.netwrote in message news:gg********************************@4ax.com...
On Tue, 24 Oct 2006 14:29:37 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
Sorry folks, the code fails on my other compilers too.

It was this code:

#include <stdio.h>

FILE *a = stderr;

int main(void)
{
return (0);
}

that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.

This one is a different story. It works on some compilers and fails
on others. It depends on the compiler's definition of the macro
"stderr". The C standard requires that stdin, stdout, and stderr are
macros that expand to pointers to file, and depending on how the
macros are defined, they may or may not be address constants at
compile time.
Hi Jack, thanks for your reply. The C90 standard says that
stdin, stdout and stderr are "expressions of type "pointer to
FILE" that point to FILE objects...". It doesn't say they are
macros.
So you should not use this, because as you found out it may work on
some implementations but not others.
I found out how the working version was implemented and
duplicated it, and it works on gcc as well.

#include <stdio.h>

extern FILE myfiles[];

FILE *a = &myfiles[0];

int main(void)
{
return (0);
}

I see what you mean - it is an address here, rather than a
variable.
The easy fix is to leave the pointer uninitialized, which means it
gets default initialized to NULL, and in main(), before it is used,
assign it.
Yes, I have basically done this now. I'm porting bwbasic
to MVS 3.8.

BFN. Paul.
Oct 24 '06 #5
Paul Edwards wrote:
The C90 standard says that stdin, stdout and stderr are
"expressions of type "pointer to FILE"
that point to FILE objects...".
It doesn't say they are macros.
The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."

--
pete
Oct 24 '06 #6
pete <pf*****@mindspring.comwrites:
Paul Edwards wrote:
>The C90 standard says that stdin, stdout and stderr are
"expressions of type "pointer to FILE"
that point to FILE objects...".
It doesn't say they are macros.

The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."
And that's a truly impressive run-on sentence (it starts more than a
page earlier).

The wording does cause some confusion. For all the other macros in
that sentence, the standard uses the phrase "which expands to" or
"which expand to"; for stderr, stdin, and stdout, is says "which are
expression ...".

I raised this a few months ago in comp.std.c:

http://groups.google.com/group/comp....54094bb4950930

The consensus was that std{err,in,out} *are* required to be macros.
(The restriction isn't necessarily useful, but there's no point in
allowing the trivial extra flexibility for implementers.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 25 '06 #7
"pete" <pf*****@mindspring.comwrote in message news:45***********@mindspring.com...
Paul Edwards wrote:
The C90 standard says that stdin, stdout and stderr are
"expressions of type "pointer to FILE"
that point to FILE objects...".
It doesn't say they are macros.

The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."
Wow!

BFN. Paul.
Oct 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
6
by: Clint Olsen | last post by:
What about the following is not computable? It seems that the size of foo is easily computable: typedef struct { char *name; char *data; } Foo; int main(void) {
2
by: Mantorok Redgormor | last post by:
struct mystruct { int a; int b; }; struct mystruct x = { 10, 10 }, y = x; What section in the standard says that y needs a constant? And why can't it resolve x so that y can have a copy of...
15
by: Scott | last post by:
Hi All, I have the following C code in a header file, outside of any functions: const float X = 50; const float Y = 100 * X; But, when compiling, I get an error: initializer element is...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
3
by: Levi Campbell | last post by:
Hi, I'm trying to debug an app someone else wrote called eMixer. Here's the log contents: cc -O3 -funroll-loops -c -o main.o main.c cc -O3 -funroll-loops -c -o nctgui.o nctgui.c cc -O3...
3
by: vib | last post by:
Hi there, I wish to get some advice on the problem I face in initializing a table. Here is the code. I've an array of strings, ptrNameString . I want to initialize a table, mySoftKeyTable...
5
by: fred | last post by:
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 {...
16
by: Gowtham | last post by:
Hi, I had some C code written which initialized a global variable as: FILE *yyerfp = stdout; This used to work fine in older versions of gcc. Now, when I tried to compile this code (with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.