Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Handling 'i...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 17 Topic 26103 of 26822
Post > Topic >>

Re: Handling 'initializer element not constant' error

by Peter Nilsson <airia@[EMAIL PROTECTED] > May 7, 2008 at 03:20 PM

Gowtham wrote:
> Hi,
>
> I had some C code written which initialized a global variable as:
>
> FILE *yyerfp = stdout;

In C, stdin, stdout and stderr are macros. They needn't be
constant expressions.

> 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

That is your error, not glibc's.

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

They don't need to be.

> As a work-around, I thought of this:
>
> FILE *yyerfp;        // Uninitialized global

Actually, it's zero initialised (to a null pointer).

> // Initialize it in a separate function
> void initializeGlobals( void )
> {
>     yyerfp = stdout;
> }
>
> int main( ... )
> {
>     // Initialize the global before doing anything else
>     initializeGlobals();
>     ...
>     // 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.

Fine, but in a sense, it's your interface that is a problem.

> If I follow this approach, I also have to add the
> initializeGlobals() call in every perl program which uses
> this library.

Replace it with a macro/function like...

#define YYERFP \
  (yyerfp ? yyerfp : (yyerfp = stdout))

  int library_foo()
  {
    FILE *fp = YYERFP;
    ...
  }

<OT> The other choice of course is C++ </OT>

> What is the best way of solving this problem?

Don't make libraries dependant on non-zero initialisation of
static variables.

--
Peter
 




 17 Posts in Topic:
Handling 'initializer element not constant' error
Gowtham <gowthamgowtha  2008-05-07 07:14:35 
Re: Handling 'initializer element not constant' error
viza <tom.viza@[EMAIL   2008-05-07 07:43:00 
Re: Handling 'initializer element not constant' error
Willem <willem@[EMAIL   2008-05-07 15:20:02 
Re: Handling 'initializer element not constant' error
richard@[EMAIL PROTECTED]  2008-05-07 17:01:29 
Re: Handling 'initializer element not constant' error
vippstar@[EMAIL PROTECTED  2008-05-07 10:29:14 
Re: Handling 'initializer element not constant' error
richard@[EMAIL PROTECTED]  2008-05-07 19:21:06 
Re: Handling 'initializer element not constant' error
Keith Thompson <kst-u@  2008-05-07 13:38:56 
Re: Handling 'initializer element not constant' error
vippstar@[EMAIL PROTECTED  2008-05-07 10:31:30 
Re: Handling 'initializer element not constant' error
vippstar@[EMAIL PROTECTED  2008-05-07 13:49:08 
Re: Handling 'initializer element not constant' error
richard@[EMAIL PROTECTED]  2008-05-07 22:29:06 
Re: Handling 'initializer element not constant' error
Ben Pfaff <blp@[EMAIL   2008-05-07 15:30:43 
Re: Handling 'initializer element not constant' error
richard@[EMAIL PROTECTED]  2008-05-07 22:40:52 
Re: Handling 'initializer element not constant' error
Ben Pfaff <blp@[EMAIL   2008-05-07 15:46:39 
Re: Handling 'initializer element not constant' error
dj3vande@[EMAIL PROTECTED  2008-05-07 22:44:24 
Re: Handling 'initializer element not constant' error
Flash Gordon <spam@[EM  2008-05-08 06:56:18 
Re: Handling 'initializer element not constant' error
Peter Nilsson <airia@[  2008-05-07 15:20:10 
Re: Handling 'initializer element not constant' error
Szabolcs Borsanyi <bor  2008-05-09 02:32:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 1:24:26 CDT 2008.