这是一个简单的C程序linklist,它显示一些运行时错误! “空指针分配”空指针赋值错误
从来没有听说过它,我没有看到代码中的任何错误?
我该怎么办?
#include <stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}node;
void main()
{
node *head,*P;
int n,i;
printf("Enter no. of elements\n");
scanf("%d",&n);
printf("Enter %d data elements\n",n);
head=(node*)malloc(sizeof(node));
scanf("%d",&(head->data));
head->next = NULL;
P=head;
for(i=1;i<n;i++)
{
P->next=(node*)malloc(sizeof(node));
P=P->next;
P->next=NULL;
scanf("%d",P->data);
}
while(P!=NULL)
{
printf("%d",P=P->data);
P=P->next;
}
}
请缩进你的代码,让我们读它。 – alk 2014-11-02 11:39:44
Yaa现在工作!其实我第一次遇到它..所以有点困惑!非常感谢! – user3672971 2014-11-02 11:44:06