473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

variable initialization / I'm a n00b

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

Jan 31 '07 #1
12 1435
"rjtucke" <rj*****@gmail.comwrote:
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
Jan 31 '07 #2
rjtucke wrote:
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/
Jan 31 '07 #3
John Smith wrote On 01/31/07 13:42,:
rjtucke wrote:
>>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
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.

--
Er*********@sun.com
Jan 31 '07 #4
Eric Sosman wrote:
John Smith wrote On 01/31/07 13:42,:
>>rjtucke wrote:

>>>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


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.
Oh, all right. s/definitive/classic. Does C90 have an ISBN? :-)
Jan 31 '07 #5
Eric Sosman said:

<snip>
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 31 '07 #6
Richard Heathfield wrote:
Eric Sosman said:

<snip>
> 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.
Hey! I resemble that comment!
Jan 31 '07 #7
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.

Jan 31 '07 #8
tp*****@mailinator.com writes:
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?
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.

--
Ben.
Jan 31 '07 #9
rjtucke wrote:
>
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
Jan 31 '07 #10
tp*****@mailinator.com wrote:
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.

Feb 1 '07 #11
On Jan 31, 5:25 pm, tphi...@mailinator.com wrote:
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.

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.

Feb 1 '07 #12
tp*****@mailinator.com wrote:

[ Please do not over-snip attributions. ]
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?
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
Feb 2 '07 #13

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

Similar topics

4
by: fortepianissimo | last post by:
We all know that using __getattr__() we can compute an instance variable on demand, for example: class Foo: def __getattr__ (self, name): if name == 'bar': self.bar = 'apple' return self.bar...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
3
by: Thomas Lenarz | last post by:
Hello, please forgive me for posting this if this problem has come up before. I could not find any hint with google. I am porting a bunch of source-code from SUN-OS to AIX at the moment and...
7
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
6
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that...
3
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains...
2
by: sh.vipin | last post by:
Below is a piece of bit manipulation code. here in function "eval_output_out" there are some macros. Say we have to versions of the same code 1. variable "b" in "eval_output_out" is initialized...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.