C: Assigning "static const char * const" to "static const char *"

c, constants, gcc, pointers, static

Solution

6.7.8/4 [C99]:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

`STRING_A` is neither, hence the error.

One to way to work around this would be along the following lines:

void input_function()
{
    static const char *current = NULL;
    if (current == NULL) {
        current = STRING_A;
    }

    ...
}

Problem

I have a program with some global strings defined at the top of the file like this: ``` static const char * const STRING_A = "STRING A"; static const char * const STRING_B = "STRING B"; ``` Then in the main program loop I repeatedly call a function. This function contains a pointer which is to point to the above strings, depending on user input. By default I want it to be set to STRING_A, so what I essentially have is this: ``` // Called repeatedly from a loop. void input_function() { static const char *current = STRING_A; // Do stuff and reassign different strings to "current" ... } ``` The problem that I have is that when compiling I get "error: initializer element is not constant". This is using GCC 4.7.2. What confuses me more is that the error goes away if I get rid of the "static" keyword in the input function. This isn't a solution though, as the static keyword is needed for the function to keep track of the current string between calls. Obviously I could fix this in many ways, most simply by just getting rid of some of the const qualifiers. But I want to understand why this isn't working. My current understanding is that the global string variables can't be modified to point to different strings, and neither can their individual characters be modified. The static keyword keeps them local to the source file. For the `current` variable in my function my understanding is that the static keyword allows it to retain its value over multiple calls to the function, and that the const qualifier in this case means that the string pointed to by `current` can change - but not the characters of the string which it points to. I'm not seeing any conflicts in those statements, so I don't understand why the compiler gives an error - particularly why it doesn't have a problem if `current`'s "static" specifier is removed. Thanks if someone can explain what the problem is here.

Original source