error C2099: initializer is not a constant problem

M

merrittr

I have some C code I am compiling in VS 2005 using the C/C++ compiler
it seems to work except for the following error



FILE *fin = stdin;
FILE *fout = stdout;

Error 1 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 18
Error 2 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 19


any ideas?
 
A

Andrey Tarasevich

merrittr said:
I have some C code I am compiling in VS 2005 using the C/C++ compiler
it seems to work except for the following error



FILE *fin = stdin;
FILE *fout = stdout;

Error 1 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 18
Error 2 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 19


any ideas?

The main idea: post more details about your code. What you posted above
lacks context, so we are forced to guess the reason for your problem. In
this case it is, fortunately, easy to guess with enough certainty.

In C language initializers for objects with static storage duration have
to be constant expressions. Apparently (and this is what we have to
guess), the above objects 'fin' and 'fout' are declared with static
storage duration (declared at file scope, most likely). Since 'stdin'
and 'stdout' are not constant expressions, they cannot be used to
initialize such objects. This is what your compiler is telling you.
 
S

Seebs

I have some C code I am compiling in VS 2005 using the C/C++ compiler
it seems to work except for the following error
FILE *fin = stdin;
FILE *fout = stdout;

Error 1 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 18
Error 2 error C2099: initializer is not a constant c:\projects\jmotion
\jmotion\jmotion.c 19


any ideas?

You appear to be declaring file-scope objects ("fin" and "fout") and trying to
initialize them with values which are not constants.

Try declaring them with "= 0", and then at the beginning of main, do:

fin = stdin;
fout = stdout;

-s
 
N

Nick Keighley

The main idea: post more details about your code. What you posted above
lacks context, so we are forced to guess the reason for your problem. In
this case it is, fortunately, easy to guess with enough certainty.

In C language initializers for objects with static storage duration have
to be constant expressions. Apparently (and this is what we have to
guess), the above objects 'fin' and 'fout' are declared with static
storage duration (declared at file scope, most likely). Since 'stdin'
and 'stdout' are not constant expressions, they cannot be used to
initialize such objects. This is what your compiler is telling you.

so he did provide enough context...
 
R

Richard Tobin

merrittr said:
I have some C code I am compiling in VS 2005 using the C/C++ compiler
it seems to work except for the following error
FILE *fin = stdin;
FILE *fout = stdout;
Error 1 error C2099: initializer is not a constant c:\projects\jmotion\jmotion\jmotion.c 18
Error 2 error C2099: initializer is not a constant c:\projects\jmotion\jmotion\jmotion.c 19

stdin and stdout need not be constants, so you can't use them to
initalise static or global variables. Do the assignments in an
initialisation function instead.

-- Richard
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Richard said:
stdin and stdout need not be constants, so you can't use them to
initalise static or global variables. Do the assignments in an
initialisation function instead.

-- Richard

According to ISO/IEC 14882:2009 Section 8.5, an initializer can be made up
of arbitrary expression so the code has no problem. I've successfully
compiled it by g++ under strict ansi mode.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkrMaVEACgkQG6NzcAXitM8E9ACfRW/XIUL2jvwvcTCASztRDvjt
O5UAn3E/aw2h5+sfwRYdAOyU0eiG75vL
=V8cU
-----END PGP SIGNATURE-----
 
J

James Kuyper

Michael said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



According to ISO/IEC 14882:2009 Section 8.5, an initializer can be made up
of arbitrary expression so the code has no problem. I've successfully
compiled it by g++ under strict ansi mode.

He said this was C code, not C++, and he posted it to comp.lang.c -
ISO/IEC 14882:2009(?) is not the relevant standard (is it a standard
yet? http://www.open-std.org/jtc1/sc22/wg21/ still lists 14882:2003 as
the current standard).
 
P

Phil Carmody

Don't quote sigs unnecessarily.
According to ISO/IEC 14882:2009 Section 8.5, an initializer can be made up
of arbitrary expression so the code has no problem. I've successfully
compiled it by g++ under strict ansi mode.

And what the heck have either of those two claims got to do with the
price of tea in china? Please evolve enough of a brain to distinguish
either between C and C++, or, if you can't do that, between comp.lang.c
and comp.lang.c++.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top