我试图在队列末尾插入节点并面临以下错误。这是编译代码时的一个简单的基本错误,但让我的生活变得艰难。
#include<stdio.h>
#include<stdlib.h>
typedef struct UNIX {
char str[20];
struct UNIX *next;
}examp;
examp *head=NULL;
int insert_last(char *s)
{
examp *new,*slide;
slide=head;
new = (examp *)malloc(sizeof(examp));
if(!new)
return(EXIT_FAILURE);
while(slide->next!=NULL)
slide=slide->next;
slide->next=new;
new->str=s;
new->next=NULL;
if(head==NULL)
{ head=new;
return 1;
}
return 1;
}
void display (void);
int main()
{
insert_last("hello ");
insert_last("how ");
insert_last("have ");
insert_last("you ");
insert_last("been ");
insert_last("! ");
display();
}
void display(void)
{
examp *slide;
slide=head;
while(slide->next!=NULL)
{ printf("%s ",slide->str);
slide=slide->next;
}
}
错误:stack_queue.c:27:10:错误:赋值给数组类型的表达式 新->str=s;
更新:使用 strncpy 解决了错误,但代码未按预期工作并意外停止。
最佳答案
你不能像那样分配给静态数组。考虑使用 strcpy
或 strncpy
来复制字符串的内容。
关于c - 在C中插入到队列的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248044/