我正在尝试打印我在 link text 中引用的单向链表的列表.它有效,但我确实收到了编译器警告:
Initialization discards qualifiers from pointer target type
(关于 start = head 的声明)和
return discards qualifiers from pointer target type
(关于返回语句)在此代码中:
/* 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;
}
我正在使用 XCode。有什么想法吗?
最佳答案
是这部分:
LIST *start = head;
函数的参数是一个指向常量的指针,const LIST *head
;这意味着您无法更改它所指向的内容。但是,上面的指针指向非常量;您可以取消引用并更改它。
它也需要是 const
:
const LIST *start = head;
这同样适用于您的返回类型。
编译器只说:“嘿,你对调用者说‘我不会改变任何东西’,但你正在为此打开机会。”
关于c - 初始化丢弃指针目标类型的限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316387/