473,768 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling 'initializer element not constant' error

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:

.../Include/Message.h:42: initializer element is not constant

I looked around the www and found that stdin/stdout/stderr are *not*
made const in the newer
versions of gcc.

As a work-around, I thought of this:

FILE *yyerfp; // Uninitialized global

// Initialize it in a separate function
void initializeGloba ls( void )
{
yyerfp = stdout;
}

int main( ... )
{
// Initialize the global before doing anything else
initializeGloba ls();
...
// Do other things
}

But, this code will also be compiled into a shared object, dynamically
loadable from other
languages such as perl etc. and I do not want to change the API
interface there. If I follow
this approach, I also have to add the initializeGloba ls() call in
every perl program which uses
this library.

What is the best way of solving this problem?

Thanks
Gowtham
Jun 27 '08
16 2791
In article <29************ *************** *******@l42g200 0hsc.googlegrou ps.com>,
<vi******@gmail .comwrote:
>I have not read much on alternative solutions to errno in C, so is
there a better solution?
Returning the error code is not possible (like pthreads) and an extra
parameter to every function that can fail would be quite annoying.
The C standard recognises the problems with errno and doesn't require
it to be an identifier. It just has to be a macro that produces a
modifiable lvalue. So a threaded implementation can do something
like

#define errno (*__thread_errn o())

where __thread_errno( ) returns a pointer to a per-thread variable.

-- Richard
--
:wq
Jun 27 '08 #11
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
The C standard recognises the problems with errno and doesn't require
it to be an identifier.
It's definitely an identifier. However, it might not be the name
of a variable with external linkage.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Jun 27 '08 #12
In article <87************ @blp.benpfaff.o rg>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>The C standard recognises the problems with errno and doesn't require
it to be an identifier.
>It's definitely an identifier.
Obviously "errno" is an identifier. I should have said that it's a
macro that doesn't have to expand to an identifier.
>However, it might not be the name of a variable with external linkage.
It might not expand to the name of a variable at all.

-- Richard
--
:wq
Jun 27 '08 #13
In article <87************ @blp.benpfaff.o rg>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>ri*****@cogsci .ed.ac.uk (Richard Tobin) writes:
>The C standard recognises the problems with errno and doesn't require
it to be an identifier.

It's definitely an identifier. However, it might not be the name
of a variable with external linkage.
errno is explicitly allowed to be a macro (n869 7.5#2).
A brief look through the definitions in n869 indicates that a macro
name is indeed covered under the standardese use of "identifier ", but
it seems that referring to a macro as an identifier in informal use is
gratuitiously confusing, especially when what the macro expands to need
not be an identifier.
dave

--
Dave Vandervies dj3vande at eskimo dot com
I would definitely not be sure that it does what the programmer
intended, even if the programmer was me.
--Christian Bau in comp.lang.c
Jun 27 '08 #14
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <87************ @blp.benpfaff.o rg>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>>The C standard recognises the problems with errno and doesn't require
it to be an identifier.
>>It's definitely an identifier.

Obviously "errno" is an identifier. I should have said that it's a
macro that doesn't have to expand to an identifier.
>>However, it might not be the name of a variable with external linkage.

It might not expand to the name of a variable at all.
Right. That's one way that it might not be the name of a
variable with external linkage.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Jun 27 '08 #15
Richard Tobin wrote, On 07/05/08 23:29:
In article <29************ *************** *******@l42g200 0hsc.googlegrou ps.com>,
<vi******@gmail .comwrote:
>I have not read much on alternative solutions to errno in C, so is
there a better solution?
Returning the error code is not possible (like pthreads) and an extra
parameter to every function that can fail would be quite annoying.

The C standard recognises the problems with errno and doesn't require
it to be an identifier. It just has to be a macro that produces a
modifiable lvalue. So a threaded implementation can do something
like

#define errno (*__thread_errn o())

where __thread_errno( ) returns a pointer to a per-thread variable.
One real example of this is current versions of glibc where after a bit
of digging I get
# define errno (* __errno_locatio n ())
--
Flash Gordon
Jun 27 '08 #16
Assuming the OP can change the code of the library, he could
initialise yyerfp to NULL and replace all the uses of it with

(yyerfp ? yyerfp : stdout)

It's a pity there isn't a standard way to get initialisation code run.

-- Richard
I'd mention here a few more poins:
- <OFF>The OP want's to initialise his shared library
(although he could avoid it with a different design). [he can
be she]
This is usally possible on a decent operating system by
defining
a function with a magic name.
</OFF>
- Global objects are not quite unusual animals (especially if they are
constant)
and one can design them using e.g. a variant of the singleton
pattern.
In C that would be to define a pointer-to-function, and one can
indeed initialise that
to an other function. In this case:

FILE *yyerrf_default () { return stdout; }
static FILE *yyerrf_ptr;
FILE *yyerrf_user() { return yyerrf_ptr; }
FILE *(*yyerrf)()=yy errf_default;
void yyerrf_set(FILE *F) { yyerrf_ptr=F; yyerrf=yyerrf_u ser; }
#define yyerrf yyerrf() // protected // don't yell // is legal in
c99
#define yyerrf_ptr // protected

(This is a slightly overcomplicated singleton, but of some
unjustified reasons I avoid conditionals.)

Szabolcs
Jun 27 '08 #17

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

Similar topics

2
19571
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 = {
2
1964
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?
3
2457
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
1393
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
7881
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) {
7
10206
by: Paul Edwards | last post by:
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)
1
1811
varuns
by: varuns | last post by:
hi i m stuck while compiling my code for generating bindings for c code to be accessible from python i have read the ext.pdf available at python.org, and i think i have done every thing right. But while compilation an error message is displayed demobox.c:495: error: initializer element is not constant demobox.c:495: error: (near initialization for `_PyDemoBox_methods.ml_meth') ......
1
4117
varuns
by: varuns | last post by:
hi i m stuck while compiling my code for generating bindings for c code to be accessible from python i have read the ext.pdf available at python.org, and i think i have done every thing right. But while compilation an error message is displayed demobox.c:495: error: initializer element is not constant demobox.c:495: error: (near initialization for `_PyDemoBox_methods.ml_meth')
2
5001
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a normal tree. Now the thing is i can popullate my tree at compile time like a global data. Since i know my tree definition at compile time, instead of using pointers to point to siblings or child nodes, i am planning to use the array
0
9407
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
10176
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...
0
10018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9964
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
8840
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...
1
7387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6656
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();...
1
3933
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3540
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.