variable initialization / I'm a n00b

Discussion in 'C Programming' started by rjtucke, Jan 31, 2007.

  1. rjtucke

    rjtucke Guest

    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
     
    rjtucke, Jan 31, 2007
    #1
    1. Advertising

  2. rjtucke

    Richard Bos Guest

    "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


    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
     
    Richard Bos, Jan 31, 2007
    #2
    1. Advertising

  3. rjtucke

    John Smith Guest

    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/
     
    John Smith, Jan 31, 2007
    #3
  4. rjtucke

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jan 31, 2007
    #4
  5. rjtucke

    John Smith Guest

    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? :)
     
    John Smith, Jan 31, 2007
    #5
  6. 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.
     
    Richard Heathfield, Jan 31, 2007
    #6
  7. 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!
     
    Clever Monkey, Jan 31, 2007
    #7
  8. rjtucke

    CBFalconer Guest

    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
     
    CBFalconer, Jan 31, 2007
    #8
  9. rjtucke

    Guest

    > > 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, 2007
    #9
  10. 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.
     
    Ben Bacarisse, Jan 31, 2007
    #10
  11. 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.
     
    Christopher Layne, Feb 1, 2007
    #11
  12. rjtucke

    Guest

    On Jan 31, 5:25 pm, 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, 2007
    #12
  13. rjtucke

    Richard Bos Guest

    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
     
    Richard Bos, Feb 2, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    950
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    465
    Ron Natalie
    Dec 11, 2005
  4. Charles
    Replies:
    6
    Views:
    426
    Zero Lee
    Jan 2, 2007
  5. Mal Ice
    Replies:
    9
    Views:
    100
    Dr John Stockton
    Sep 4, 2004
Loading...

Share This Page