warning: initializer element is not computable at load time

This is a discussion on warning: initializer element is not computable at load time within the C Programming forums, part of the General Programming Boards category; What does this warning mean? Code: 12.12.c:54: warning: initializer element is not computable at load time 12.12.c:54: warning: initializer element ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    warning: initializer element is not computable at load time

    What does this warning mean?
    Code:
    12.12.c:54: warning: initializer element is not computable at load time
    12.12.c:54: warning: initializer element is not computable at load time

    Look at the bolded code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stackNode {
    	char data;
    	struct stackNode *nextPtr;
    };
    
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    
    
    void convertToPostfix( char infix[], char postfix[] );
    //isOperator completed
    int isOperator( char c );
    //precedence completed
    int precedence( char operator1, char operator2 );
    void push( StackNodePtr *topPtr, char value );
    char pop( StackNodePtr *topPtr );
    char stackTop( StackNodePtr topPtr );
    int isEmpty( StackNodePtr topPtr );
    void printStack( StackNodePtr topPtr );
    
    
    
    int main()
    {
    
    	printf( "%d\n", precedence( '-', '^' ) );
    //	printf( "%d%10d\n%d%10d\n%d%10d\n%d%10d\n", precedence(
    
    	return 0;
    }
    
    
    int isOperator( char c )
    {
    	if ( c == '(' || c == ')'
           || c == '+' || c == '-'
           || c == '*' || c == '/'
    	  || c == '^' || c == '%' )
    		return 1;
    
    	else
    		return 0;
    
    
    }
    
    
    //Determine if precedence of operator1 is greater(1), equal(0), or less then(-1) operator2
    int precedence( char operator1, char operator2 )
    {
    	int opValue[ 2 ] = { 0 };
    	char operator[ 2 ] = { operator1, operator2 };
    	int i = 0;
    
    	for ( i = 0; i < 2; ++i ) {
    		switch( operator[ i ] ) {
    			case '+': case '-':
    				opValue[ i ] = 1;
    				break;
    			case '*': case '/': case '%':
    				opValue[ i ] = 2;
    				break;
    			case '^':
    				opValue[ i ] = 3;
    				break;
    			case '(': case ')':
    				opValue[ i ] = 4;
    				break;
    		}
    		printf( "opValue[ %d ]: %d\n", i, opValue[ i ] );
    	}
    
    
    
    	if ( opValue[ 0 ] > opValue[ 1 ] )
    		return 1;
    	if ( opValue[ 0 ] == opValue[ 1 ] )
    		return 0;
    	if ( opValue[ 0 ] < opValue[ 1 ] )
    		return -1;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With MSVC I get:

    main.cpp(55) : warning C4204: nonstandard extension used : non-constant aggregate initializer
    main.cpp(55) : warning C4204: nonstandard extension used : non-constant aggregate initializer
    main.cpp(84) : warning C4715: 'precedence' : not all control paths return a value
    The first two warnings indicate that the standard way would be:

    Code:
    char operator[ 2 ];
    operator[0] = operator1;
    operator[1] = operator2;
    The last warning should be wrong (the compiler doesn't realize that you have all cases covered), but you might use if...else if...else instead (or perhaps just return opValue[0] - opValue[1];
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahh thank you.

    This brings up a more precise question.
    1. Which part of the statement is the "initializer element" ?
    2. What does it mean that it's "non-computable at load-time" ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by yougene
    1. Which part of the statement is the "initializer element" ?
    The parts in red:
    Code:
    char operator[ 2 ] = { operator1, operator2 };
    Quote Originally Posted by yougene
    2. What does it mean that it's "non-computable at load-time" ?
    The values of those elements are only known at run time. It is not actually true in this case, but it could be true if you call precedence() using values obtained from the user.

    Incidentally, note that precedence() has a control path that does not return a value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Incidentally, note that precedence() has a control path that does not return a value. Well, technically, yes; practically, no. [edit] what the heck is up with the boards formatting engine? [/code]
    Last edited by Sebastiani; 01-16-2009 at 01:01 PM. Reason: line wrapping
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    1. Will this cause problems if I do pass user-input?

    2. The function is going to be used in conjunction with isOperator().

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by Sebastiani
    Incidentally, note that precedence() has a control path that does not return a value. Well, technically, yes; practically, no.
    Yeah, now I see that anon has covered that. My eyes were fooled by a sequence of if statements with returns but no return at the end. Consdering that, I would say that the code is wrong in a sense: the last if should be an else, or removed entirely.

    Quote Originally Posted by yougene
    1. Will this cause problems if I do pass user-input?
    Since a compiler extension is in use, no. However it may cause problems if you intend to port the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Consdering that, I would say that the code is wrong in a sense: the last if should be an else, or removed entirely. I agree completely. Besides, a slight change to the code could lead to a nasty bug. If nothing else, it's bad form...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    182
    What would be good form?

    I was originally going to use if, else if, else. Both have the same short-coming in my mind although in different ways.

    The way I did it has the possibility of not returning anything
    Using else has the possibility of not returning a correct value

    As the program stands neither concern is valid since a number is always positive, 0, or negative.

    My value judgment was which is worse? "Having a function not return anything which is easily caught or having a function return the wrong value (else statement) which could have been more difficult." The function is pretty trivial and it probably doesn't require this hypothetical but there it is. Is my reasoning partial or flawed?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by yougene
    The way I did it has the possibility of not returning anything
    Using else has the possibility of not returning a correct value
    The possibly of not returning a correct value always exists, though it can be mitigated with repeatable tests (and formal verification, if necessary).

    Incidentally, if you follow the convention of merely comparing with 0, you could just write:
    Code:
    return opValue[ 0 ] - opValue[ 1 ];
    EDIT:
    Concerning your original question, I think that it may be better to do it this way:
    Code:
    /* Return precedence value of operator1 as a positive integer,
     * or 0 if operator1 is invalid.
     */
    int precedence( char operator1 )
    {
        switch( operator1 )
        {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
        case '%':
            return 2;
        case '^':
            return 3;
        case '(':
        case ')':
            return 4;
        default:
            return 0;
        }
    }
    
    /* Return a positive integer, zero, or a negative integer if the precedence of
     * operator1 is greater than, equal to, or less than that of operator2 respectively.
     * The operators are assumed to be valid.
     */
    int comparePrecedence( char operator1, char operator2 )
    {
        return precedence( operator1 ) - precedence( operator2 );
    }
    Last edited by laserlight; 01-16-2009 at 02:13 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    int isOperator( char c )
    {
    	if ( c == '(' || c == ')'
           || c == '+' || c == '-'
           || c == '*' || c == '/'
    	  || c == '^' || c == '%' )
    		return 1;
    
    	else
    		return 0;
    
    
    }
    Might I suggest:
    Code:
    int isOperator( char c )
    {
        return ( c == '(' || c == ')'
            || c == '+' || c == '-'
            || c == '*' || c == '/'
            || c == '^' || c == '%' );
    }
    (Note the indentation, yours isn't very good!)
    cpwiki.sf.net/Indentation

    Or if you want to be fancy, you could use something like
    Code:
    int isOperator( char c ) {
        return strchr("()+-*/^%", c) != NULL;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    Quote Originally Posted by yougene View Post
    What would be good form?

    I was originally going to use if, else if, else. Both have the same short-coming in my mind although in different ways.

    The way I did it has the possibility of not returning anything
    Using else has the possibility of not returning a correct value

    As the program stands neither concern is valid since a number is always positive, 0, or negative.

    My value judgment was which is worse? "Having a function not return anything which is easily caught or having a function return the wrong value (else statement) which could have been more difficult." The function is pretty trivial and it probably doesn't require this hypothetical but there it is. Is my reasoning partial or flawed?
    Consider:
    Code:
    if (x) return x;
    if (y) return y;
    if (z) return z;
    ...and...
    Code:
    if (x) return x;
    else if (y) return y;
    else return z;
    The latter form will stop the compiler from thinking there might another case you have forgotten. But they are also identical.
    There is no drawback the the latter one, which you should be well aware of.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if(X) return x;
    if(Y) return y;
    return z;
    will also get rid of the warning if you are sure that X, Y and Z represent all the available choices
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 07:32 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21