Initialization discards qualifiers from pointer target type

后端 未结 2 1720
暗喜
暗喜 2020-12-09 14:47

I\'m trying to print the list of a singly linked list that I referred to in link text. It works, but I do get the compiler warnings:

Initializat

2条回答
  •  盖世英雄少女心
    2020-12-09 15:27

    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;
      }
      

提交回复
热议问题