473,703 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initializer element is not constant

On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initialize r 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 10194
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******@nospp aam.w3.towrote in message
news:45******** **************@ un-2park-reader-01.sydney.pipen etworks.com.au. ..
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initialize r 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******@nospp aam.w3.towrote in comp.lang.c:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initialize r 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.l earn.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******@nospp aam.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 24 '06 #4
"Jack Klein" <ja*******@spam cop.netwrote in message news:gg******** *************** *********@4ax.c om...
On Tue, 24 Oct 2006 14:29:37 +1000, "Paul Edwards"
<ke******@nospp aam.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 "expression s 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
"expression s 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*****@mindsp ring.comwrites:
Paul Edwards wrote:
>The C90 standard says that stdin, stdout and stderr are
"expression s 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_Keit h) 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*****@mindsp ring.comwrote in message news:45******** ***@mindspring. com...
Paul Edwards wrote:
The C90 standard says that stdin, stdout and stderr are
"expression s 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
19563
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
16978
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
1960
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 its values?
15
699
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 not constant
4
4115
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", PRO_REQUIRED }; const char* TOS_network_spantree_set_0_para_1_emvalue = { "disable", "enable", NULL }; PARA TOS_network_spantree_set_0_para_1 = { "", emENUM, TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP", PRO_REQUIRED };
3
2454
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 -funroll-loops -c -o mixer.o mixer.c mixer.c: In function `open_soundcard_alsa':
3
1391
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 with some contents of the string from the array. This initialization is not in any function, but rather outside of any function. However, the compiler complaints, the related lines( L1, L2, L3, L4) of "Initializer element is not constant". I
5
7878
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 { int a; int b;} t; int main(int argc, char** argv) {
16
2774
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 gcc 3.2.3), I got errors like:
0
8654
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5910
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2406
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2037
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.