What is wrong with this declaration?
char *add_element[] = {"1","S"};
I get this error when I compile this –
warning: initialization discards qualifiers from pointer target type
What am I doing wrong?
This question is different from Why I get; initializing 'char *' with an expression of type 'const char *' discards qualifiers?. This can be verified by comment written below. Thanks for answering it.
The possible duplicate question is related, but not the same. It is about why void func(const char *ptr) { char *local = ptr; … } elicits the warning, rather than dealing with an initializer as here. I don’t think this question should be closed as a duplicate of that question
You appear to be using GCC and have
-Write-stringsturned on. That makes the compiler warn about exactly this situation. It makes the string literals intoconst chararrays rather thanchararrays, making your initialization discard theconst. Use:Or turn off
-Wwrite-strings.From the GCC manual: