Initialization discards qualifiers from pointer target type Initialization discards qualifiers from pointer target type c c

Initialization discards qualifiers from pointer target type


It's this part:

LIST *start = head;

The parameter for the function is a pointer to a constant, const LIST *head; this means you cannot change what it is pointing to. However, the pointer above is to non-const; you could dereference it and change it.

It needs to be const as well:

const LIST *start = head;

The same applies to your return type.


All the compiler is saying is: "Hey, you said to the caller 'I won't change anything', but you're opening up opportunities for that."


In following function, would get the warning that you encountered with.

void test(const char *str) {  char *s = str;}

There are 3 choices:

  1. Remove the const modifier of param:

    void test(char *str) {  char *s = str;}
  2. Declare the target variable also as const:

    void test(const char *str) {  const char *s = str;}
  3. Use a type convert:

    void test(const char *str) {  char *s = (char *)str;}