想插入链表,怎么无法运行

#include<stdio.h>
struct a
{
        int element;
        struct a *next;
};
void insert(int a,struct a *P)
{
    struct a *TmpCell;
    TmpCell =(struct a*)malloc(10);
    TmpCell->element=a;
    TmpCell->next=P->next;
    P->next=TmpCell;
}
int main(void)
{
    int b=1;
    struct a *P;
    P->next=NULL;
    insert(b,P);
    return 0;



}
阅读 1.5k
1 个回答

起码贴一下运行日志啊。
目前我看到的错误有三个地方
malloc 函数需要引入 stdlib.h
main函数中的指针P没有初始化
TmpCell =(struct a*)malloc(10); 这里这样写不合理
TmpCell =(struct a*)malloc(sizeof(struct a)); 更好一些

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进