我只允许使用下列头scanf函数的char *字结构只显示最后输入
#include <stdio.h>
#include <stdlib.h>
,我定义我的struct student
如下:
struct dict
{
char* word;
struct dict* link;
};
有很多功能,但只有一个功能,我现在有问题。 该函数在链接的末尾插入一个结构字典,并带有某个名称。
struct student *Linsert(struct dict *list, char *name)
{
struct student *pnew;
struct student *pn;
int exist = 1;
pnew = (struct dict *)malloc(sizeof(struct dict));
pnew -> next = NULL;
pnew -> name = name;
if (list != NULL)
{
for (pn = list; pn -> next != NULL; pn = pn -> next) ;
pn -> next = pnew;
}
else
list = pnew;
return list;
}
使用下面的函数,
//print all the values in the list void printList(struct dict* list);
我这样做:
int main(void)
{
struct dict *list = NULL;
char *name;
while (1) {
scanf("%s", name);
if (name == 'Q')
break;
list = Linsert(list, name);
printList(list);
}
return 0;
}
比方说输入,我输入3 apple
banana
和orange
,我的结果显示了我最后的三个输入。
这里有什么问题?
始终用**所有警告**进行编译,并且不要忽略其中的任何警告。 –
请看看[如何格式化您的问题](http://stackoverflow.com/editing-help),特别是下一次如何格式化代码块。 –
@KerrekSB'-Wall -Wextra -Werror'总是会导致更好的代码':)' –