Initialization discards qualifiers from pointer target type

后端 未结 2 1715
暗喜
暗喜 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:20

    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."

提交回复
热议问题