Thread: initializer element is not constant...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    20

    Unhappy initializer element is not constant...

    Hi
    I'm trying to compile the following code, but gcc 4.2 returns the messages
    "global.c:8: error: initializer element is not constant"
    "global.c:9: error: initializer element is not constant"

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    /*file global.c*/
    
    static long double h=1e-2;
    static long double t_max=2.0*3.1415926535897932384626433832795;
    static long double y_max=100.0;
    static unsigned int lin=(int)(y_max/h+2);
    static unsigned int col=(int)(t_max/h+1);
    
    int main()
    {
       return 0;  
    }
    Can anyone help me? Nothing that I read on the subject is much clear about this...

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you want global variables just remove the static bit:
    Code:
    long double h=1e-2;
    Or if you want constants use a define:
    Code:
    #define t_max 2.0*3.1415926535897932384626433832795

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    With #define it would probably be better to enclose the expression with parentheses. A macro name would also be typically uppercase.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Ok.
    As a matter of fact I want them to be global and static as well.
    I read about the #define environment to create macros but I'm not sure if
    Code:
    #define  t_max 2.0*3.1415926535897932384626433832795
    results in a floating point t_max or what.

    Thanks.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, the lines it's complaining about are lin and col. There's no reason the compiler should think that an expression like (int)(t_max/h+1) is constant, since it isn't. You could wrap it all up in parentheses and put it in a #define macro as per above.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by John Connor View Post
    Ok.
    As a matter of fact I want them to be global and static as well.
    I read about the #define environment to create macros but I'm not sure if
    Code:
    #define t_max  t_max 2.0*3.1415926535897932384626433832795
    results in a floating point t_max or what.

    Thanks.
    That's going to get you a lot of errors.

    #define macros are just search-n-replace; if you did
    Code:
    #define t_max (2.0*3.1415926535897932384626433832795)
    would simply make the preprocessor remove all occurrences of the token t_max and replace it with that ... thing on the right. The compiler would never see a variable t_max at all.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by tabstop View Post
    That's going to get you a lot of errors.

    #define macros are just search-n-replace; if you did
    Code:
    #define t_max (2.0*3.1415926535897932384626433832795)
    would simply make the preprocessor remove all occurrences of the token t_max and replace it with that ... thing on the right. The compiler would never see a variable t_max at all.

    Thanks I realised my mistake in the previos reply and removed the t_max t_max...

    Now, if I use the expression
    Code:
    #define t_max 2.0*3.1415926535897932384626433832795
    Can i call t_max expecting it to be double precision?

    Thanks

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by John Connor View Post
    Thanks I realised my mistake in the previos reply and removed the t_max t_max...

    Now, if I use the expression
    Code:
    #define t_max 2.0*3.1415926535897932384626433832795
    Can i call t_max expecting it to be double precision?

    Thanks
    Yes, in that any constant with a decimal point in it in a C program is double unless you tag it otherwise: 1.0 is double, 1.0f is float.
    Last edited by tabstop; 02-01-2008 at 12:24 PM. Reason: fixed

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Yes, in that any constant with a decimal point in it in a C program is double unless you tag it otherwise: 1.0 is double, 1.0f is float.
    Thanks a lot! That really helped me.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by John Connor View Post
    As a matter of fact I want them to be global and static as well.
    I don't think you know what you mean by your wanting constants to be static. What are you expecting this to mean for them, and why?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by iMalc View Post
    I don't think you know what you mean by your wanting constants to be static. What are you expecting this to mean for them, and why?
    Hi
    At the first time, wen I wrote the code
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    /*file global.c*/
    
    static long double h=1e-2;
    static long double t_max=2.0*3.1415926535897932384626433832795;
    static long double y_max=100.0;
    static unsigned int lin=(int)(y_max/h+2);
    static unsigned int col=(int)(t_max/h+1);
    
    int main()
    {
       return 0;  
    }
    h,t_max,y_max, lin and col were all global "variables". Weren't they?

    Now wen a say global and static I mean that they can all be accessed by whatever function my program can possibly have, but can' be reassigned by any of them.

    But I Didn't get it, I think...

    Thanks

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, static doesn't mean what you think it does. const is what you are after.

    static means that the variable can't be seen outside the current compilation unit (that's a source-file in most cases, but if you do weird stuff, it may be different).

    And I think the error you are getting is caused by the fact that the compiler can't quite figure out your rather complicated math in
    Code:
    static unsigned int lin=(int)(y_max/h+2);
    static unsigned int col=(int)(t_max/h+1);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by matsp View Post
    Right, static doesn't mean what you think it does. const is what you are after.

    static means that the variable can't be seen outside the current compilation unit (that's a source-file in most cases, but if you do weird stuff, it may be different).

    And I think the error you are getting is caused by the fact that the compiler can't quite figure out your rather complicated math in
    Code:
    static unsigned int lin=(int)(y_max/h+2);
    static unsigned int col=(int)(t_max/h+1);
    --
    Mats
    Hi, thanks for the tip.
    The program is running ok after I changed things based on what was told here...

    Sorry for bothering you all with newbie questions, but I think I'm starting to learn it right... I have read a lot but I have never practiced it...

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM