c - 错误,使用C编程的链表

标签 c linked-list push singly-linked-list

我正在尝试编写简单的推送功能,一切都很好,直到运行时,执行的代码崩溃。谁能解释一下原因吗?

#include<stdio.h>
#include<stdlib.h>

typedef struct list{
int order;
struct list *next;

}list;

void push(struct list **arg,int i);

int main()
   {
struct list **ptr=NULL;

for(int i=0;i<10;++i){
    push(ptr,i);
  }

return 0;
     }

void push(struct list **arg,int i){

 struct list *temp;
 temp= malloc(sizeof(list));

temp->order=i;

temp->next=*arg;

*arg=temp;

}

最佳答案

list *ptr=NULL;
     ^^^           
for(int i=0;i<10;++i){
    push( &ptr,i);
         ^^^^
  }

否则如果这样声明

struct list **ptr=NULL;

然后这个解除引用

*ptr

导致未定义的行为。

关于c - 错误,使用C编程的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41433277/

相关文章:

c - 如何在 switch 语句的 case 字段中使用变量?

java - 在java中传输二进制文件(数据)

c - 最大标识符长度

c - 从客户端向服务器发送一个字符串

java - ArrayList 与数组和列表的比较

git - 从 Linux 上的裸 git 存储库 check out 选定文件和文件夹的最佳方法是什么?

git aws.push : 'aws.push' is not a git command

java - Java 升序循环链表

performance - 用于实现堆栈的链表与动态数组

php - MongoDB和PHP可以添加同名字段吗?