variable initialization / I'm a n00b

R

rjtucke

Hi all- this is my first post here- just a quick question:

#include <math.h>
double foo = cosh(0.621);
int main() {
return 0;
}

fails with

error: initializer element is not constant

In addition to telling me what I'm doing wrong, could you please refer
me to an appropriate reference for related questions? I've been doing
my reading (open net + safari bookshelf), but I don't know where to
look. Even if I have to (sigh) buy a book, I would appreciate your
pointers.

Thanks and regards,
Ross
 
R

Richard Bos

rjtucke said:
Hi all- this is my first post here- just a quick question:

#include <math.h>
double foo = cosh(0.621);
int main() {
return 0;
}

fails with

error: initializer element is not constant

Yes? Initialisers for static-duration objects (i.e., "global" objects,
and "local" objects you've declared using static) must be compile-time
constants, and a function call is not a compile-time constant, even if
all its arguments are.
In addition to telling me what I'm doing wrong, could you please refer
me to an appropriate reference for related questions?

K&R.

Richard
 
J

John Smith

rjtucke said:
Hi all- this is my first post here- just a quick question:

#include <math.h>
double foo = cosh(0.621);
int main() {
return 0;
}

fails with

error: initializer element is not constant

In addition to telling me what I'm doing wrong, could you please refer
me to an appropriate reference for related questions? I've been doing
my reading (open net + safari bookshelf), but I don't know where to
look. Even if I have to (sigh) buy a book, I would appreciate your
pointers.

Thanks and regards,
Ross

"K&R" is "The C Programming Language" by Brian Kernighan & Dennis
Ritchie. Ritchie is the inventor of C and this is the definitive
book on the language. If you are already familiar with
programming in general, it's a good place to start learning C. If
not, there are books better oriented to beginners.

Online (among many other good sites) see:
http://c-faq.com/
http://clc-wiki.net/wiki/
 
E

Eric Sosman

John Smith wrote On 01/31/07 13:42,:
"K&R" is "The C Programming Language" by Brian Kernighan & Dennis
Ritchie. Ritchie is the inventor of C and this is the definitive

s/this is/this was/
book on the language. If you are already familiar with
programming in general, it's a good place to start learning C. If
not, there are books better oriented to beginners.

K&R ceased to be the definitive work on C some seventeen
and a half years ago. Still, it's better reading than the
more recent definitive works.
 
J

John Smith

Eric said:
John Smith wrote On 01/31/07 13:42,:



s/this is/this was/




K&R ceased to be the definitive work on C some seventeen
and a half years ago. Still, it's better reading than the
more recent definitive works.

Oh, all right. s/definitive/classic. Does C90 have an ISBN? :)
 
R

Richard Heathfield

Eric Sosman said:

K&R ceased to be the definitive work on C some seventeen
and a half years ago.

It is, however, *a* definitive work on C, in the eyes of many working
programmers the world over who have never so much as glanced at the
Standard, but whose well-thumbed K&Rs are an indispensable feature of their
desktops.
 
C

Clever Monkey

Richard said:
Eric Sosman said:



It is, however, *a* definitive work on C, in the eyes of many working
programmers the world over who have never so much as glanced at the
Standard, but whose well-thumbed K&Rs are an indispensable feature of their
desktops.
Hey! I resemble that comment!
 
C

CBFalconer

rjtucke said:
Hi all- this is my first post here- just a quick question:

#include <math.h>
double foo = cosh(0.621);
int main() {
return 0;
}

fails with

error: initializer element is not constant

In addition to telling me what I'm doing wrong, could you please
refer me to an appropriate reference for related questions? I've
been doing my reading (open net + safari bookshelf), but I don't
know where to look. Even if I have to (sigh) buy a book, I would
appreciate your pointers.

You are trying to initialize an object with the return value of a
function, which is not a constant. Just what the error message
told you.

Search for n1124.pdf, which is effectively the 1999 ISO C
standard. Also buy "The C Programming Language", by Kernighan and
Ritchie, 2nd edition.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
T

tphipps

error: initializer element is not constant
Yes? Initialisers for static-duration objects (i.e., "global" objects,
and "local" objects you've declared using static) must be compile-time
constants, and a function call is not a compile-time constant, even if
all its arguments are.

Of course one can write a function involving a global variable which
cannot
be evaluated sensibly at compile time; however, surely if the compiler
is clever
enough then it could work out that it could evaluate cosh only using
the arguments
to the function and automatic variables, and helpfully insert the
result as a
compile-time constant? Of course it should still throw an error if it
can't prove
that it can correctly evaluate the return value from compile-time
information.
 
B

Ben Bacarisse

Of course one can write a function involving a global variable which
cannot be evaluated sensibly at compile time; however, surely if the
compiler is clever enough then it could work out that it could
evaluate cosh only using the arguments to the function and automatic
variables, and helpfully insert the result as a compile-time
constant?

Indeed it could, but then it would not be (in some sense) a C
compiler. It is the specification of the language that prohibits it,
not the difficulty (or otherwise) of implementing it.

PS. Please take care to keep attributions the "tphipps... writes:"
line at the start of your reply.
 
C

Christopher Layne

Of course one can write a function involving a global variable which
cannot
be evaluated sensibly at compile time; however, surely if the compiler
is clever
enough then it could work out that it could evaluate cosh only using
the arguments
to the function and automatic variables, and helpfully insert the
result as a
compile-time constant? Of course it should still throw an error if it
can't prove
that it can correctly evaluate the return value from compile-time
information.

And yet writing a simple init function to do it at runtime is both more
logical, less surprising, and correctly portable.
 
R

robertwessel2

Of course one can write a function involving a global variable which
cannot
be evaluated sensibly at compile time; however, surely if the compiler
is clever
enough then it could work out that it could evaluate cosh only using
the arguments
to the function and automatic variables, and helpfully insert the
result as a
compile-time constant? Of course it should still throw an error if it
can't prove
that it can correctly evaluate the return value from compile-time
information.


It cannot for a compile time initializer, since the language spec
prohibits it, but there's nothing preventing it for doing so in other
contexts. For example, MSVC (like many other compilers), can inline
many of the smaller library functions, so it reduces
"i=strlen("abcdef");) to a move of a constant six.
 
R

Richard Bos

(e-mail address removed) wrote:

[ Please do not over-snip attributions. ]
Of course one can write a function involving a global variable which
cannot
be evaluated sensibly at compile time; however, surely if the compiler
is clever
enough then it could work out that it could evaluate cosh only using
the arguments
to the function and automatic variables, and helpfully insert the
result as a
compile-time constant?

No, it couldn't. The Standard defines what is a compile-time constant
and what is not. At most, it could allow this as an extension, and
continue the compilation with a diagnostic message; but the Standard
demands that the diagnostic is given, no matter how clever the
implementation thinks it is.

Richard
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top