初始化丢弃了指针目标类型的限定 [英] Initialization discards qualifiers from pointer target type
问题描述
我想打印我的链接文本
它的工作原理,但我得到的编译器警告
It works, but I do get the compiler warnings
初始化丢弃了指针目标类型的限定(启动=头的声明),并返回指针目标类型丢弃限定(在return语句)本code(我用X code)
"Initialization discards qualifiers from pointer target type"(on declaration of start = head) and return discards qualifiers from pointer target type"(on return statement) in this code (I am using XCode):
/* Prints singly linked list and returns head pointer */
LIST *PrintList(const LIST *head)
{
LIST *start = head;
for (; start != NULL; start = start->next)
printf("%15s %d ea\n", head->str, head->count);
return head;
}
有什么想法?谢谢!
Any thoughts? Thanks!
推荐答案
这是这一部分:
LIST *start = head;
该函数的参数是一个指向一个常量,常量LIST *头
;这意味着你不能改变它指向。然而,上面的指针指向非const;你可以解引用它,改变它。
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.
它需要常量
以及
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."
这篇关于初始化丢弃了指针目标类型的限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!