c - 在C中插入到队列的末尾

标签 c linked-list stack queue

我试图在队列末尾插入节点并面临以下错误。这是编译代码时的一个简单的基本错误,但让我的生活变得艰难。

#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 解决了错误,但代码未按预期工作并意外停止。

最佳答案

你不能像那样分配给静态数组。考虑使用 strcpystrncpy 来复制字符串的内容。

关于c - 在C中插入到队列的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248044/

相关文章:

c - 链表添加节点更改所有节点值,而不仅仅是一个

c - .so 库是否需要在所有情况下都在运行时存在

使用 C 将字符串中的所有其他字母字符更改为大写

C : Printing a pointer to an array seems to print a junk value also

C - 分配指向特定位置的指针

Java:替代 LinkedList 或 int 指针数据结构

java - 如何在 Java 中实现有界堆栈

macos - 为什么我的 x86 链表函数会抛出段错误?

javascript - JS 实现栈时clear方法不清楚

c - 将字符串数组传递出c中的函数