5

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

Community
  • 1
  • 1
fatrock92
  • 827
  • 1
  • 8
  • 16
  • More often than not, searching Stack Overflow for the error message gets you a good answer. – Sergey Kalinichenko Feb 10 '13 at 16:56
  • 1
    @dasblinkenlight I'm going to print that comment of yours and put it in a golden frame. –  Feb 10 '13 at 16:57
  • 3
    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. – Jonathan Leffler Feb 10 '13 at 17:01
  • What compiler/environment? Can you show us your command line? – Carl Norum Feb 10 '13 at 17:09

1 Answers1

9

You appear to be using GCC and have -Write-strings turned on. That makes the compiler warn about exactly this situation. It makes the string literals into const char arrays rather than char arrays, making your initialization discard the const. Use:

const char *add_element[] = { "1", "S" };

Or turn off -Wwrite-strings.

From the GCC manual:

-Wwrite-strings

When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning. These warnings will help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it will just be a nuisance. This is why we did not make -Wall request these warnings.

When compiling C++, warn about the deprecated conversion from string literals to char *. This warning is enabled by default for C++ programs.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Somebody here on SO told me that string literals are `char[]` for historical reasons. Now what? –  Feb 10 '13 at 16:54
  • @JanDvorak `const char *add_element[]`. –  Feb 10 '13 at 16:54
  • @H2CO3, that's true. They just decay into pointers in the initialization. I could clarify, I guess, but that's a pretty subtle point for a beginner. – Carl Norum Feb 10 '13 at 16:56
  • However, even I answered that it's `const char *`. I'm really confused. –  Feb 10 '13 at 16:56
  • this question is tagged C and string literals in C are not `const char[N]` but `char[N]`. – ouah Feb 10 '13 at 17:03
  • @ouah, in this case the OP must have something (compiler? flag?) that make them `const`, or he wouldn't get that warning. – Carl Norum Feb 10 '13 at 17:03
  • Some googling seems to indicate older G++, maybe? I'll update my answer in a bit. – Carl Norum Feb 10 '13 at 17:13
  • @CarlNorum my guess is also that the compiler is a C++ compiler or that the error does not come from this line. You cannot make string literals `const char[N]` without breaking a lot of things. – ouah Feb 10 '13 at 17:15
  • 1
    Found it: `-Wwrite-strings` to gcc generates this message. – Carl Norum Feb 10 '13 at 17:15
  • And clang will warn when given that flag too, but gives a more verbose message. – Carl Norum Feb 10 '13 at 17:21