Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > variable initialization / I'm a n00b

Reply
Thread Tools

variable initialization / I'm a n00b

 
 
rjtucke
Guest
Posts: n/a
 
      01-31-2007
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

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      01-31-2007
"rjtucke" <(E-Mail Removed)> 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


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
 
Reply With Quote
 
 
 
 
John Smith
Guest
Posts: n/a
 
      01-31-2007
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/
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-31-2007
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      01-31-2007
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?
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-31-2007
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.
 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      01-31-2007
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!
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-31-2007
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


 
Reply With Quote
 
tphipps@mailinator.com
Guest
Posts: n/a
 
      01-31-2007
> > 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.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-31-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[n00b] Variable with space is split Charles C++ 6 01-02-2007 08:41 AM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM
Using / passing a variable - n00b question :) Mal Ice Javascript 9 09-04-2004 04:01 PM



Advertisments