Thread: Iinitializer element is not constant - why is that?

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    183

    Iinitializer element is not constant - why is that?

    Hello,

    for code as below:


    Code:
    #define GET_TX_BASE()  DEBUG_TX_BASE 
    #define DEBUG_RX_BASE 0xBE000000
    #define DEBUG_TX_BASE 0xBEFE0000
    
    volatile struct ring * const rx = (void *) DEBUG_RX_BASE;
    volatile struct ring * const tx = (void *) GET_TX_BASE();
    GCC complains about the initializer GET_TX_BASE() which is a function like macro - it will end up to be a constant at the end of the day. OF course gcc is OK with initializer of DEBUG_RX_BASE. So why is GCC so stupid and won't compile it? Also what is this error all about? Why GCC thinks this constant is not constant?


    Thanks
    Last edited by MartinR; 11-19-2018 at 08:38 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    688
    How could the code you've posted not compile? The error must be elsewhere.
    Post a full program that demonstrates the error.
    Show your compile line.
    Post the exact text of the error.
    Last edited by john.c; 11-19-2018 at 08:45 AM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    183
    @john.c, the exact gcc error about line 6 says "error: initializer element is not constant".

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    688
    How could the code you've posted not compile? The error must be elsewhere.
    Post a full program that demonstrates the error.
    Show your compile line.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,231
    Quote Originally Posted by MartinR
    @john.c, the exact gcc error about line 6 says "error: initializer element is not constant".
    To illustrate john.c's point, I compiled this program with gcc 5.4.0 with options -Wall -pedantic -ansi, and also compiled it again with -ansi swapped out for -std=c99, and in both cases gcc neither issued a warning nor an error message:
    Code:
    #define GET_TX_BASE()  DEBUG_TX_BASE
    #define DEBUG_RX_BASE 0xBE000000
    #define DEBUG_TX_BASE 0xBEFE0000
    
    volatile struct ring * const rx = (void *) DEBUG_RX_BASE;
    volatile struct ring * const tx = (void *) GET_TX_BASE();
    
    int main(void)
    {
        return 0;
    }
    Consequently, your claim that gcc emitted an error message concerning line 6 must be wrong, unless you can produce a program that demonstrates the contrary.
    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

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    688
    Yes, that's exactly my point. I need to be more clear and less "jokey".
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer element is not constant
    By Athul in forum C Programming
    Replies: 6
    Last Post: 10-22-2018, 09:05 AM
  2. "initializer element is not constant" error
    By dukester in forum C Programming
    Replies: 24
    Last Post: 05-18-2011, 12:03 PM
  3. initializer element is not constant error - help
    By jay32m in forum C Programming
    Replies: 7
    Last Post: 11-17-2010, 03:24 PM
  4. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 06:28 PM
  5. initializer element is not constant
    By lucaspewkas in forum C Programming
    Replies: 4
    Last Post: 05-20-2005, 05:21 AM

Tags for this Thread