主题:关于二级指针的问题
#include<stdio.h>
#include<stdlib.h>
struct pcb
{
int pid;
int priority;
struct pcb *next;
};
void create(struct pcb**head)
{
*head=(struct pcb*)malloc(sizeof(struct pcb)); //???
(*head)->next=NULL;
}
void insert(struct pcb**head,struct pcb*stu)
{
struct pcb*curent;
curent=(*head)->next;
if(curent==NULL)
{
curent=stu;
(*head)->next=stu;
curent->next=NULL;
}
while(curent->next!=NULL)
{
curent=curent->next;
}
stu->next=curent->next;
curent->next=stu;
}
void print(struct pcb**head)
{
struct pcb*curent;
curent=*head;
while(curent!=NULL)
{
printf("%d",curent->pid);
curent=curent->next;
}
}
int main()
{
struct pcb**h;
struct pcb*stu1;
struct pcb*stu2;
stu1=(struct pcb*)malloc(sizeof(struct pcb));
stu2=(struct pcb*)malloc(sizeof(struct pcb));
stu1->pid=0;
stu2->pid=1;
create(h);
// free(h);
return 0;
}
编译过程发现*head=(struct pcb*)malloc(sizeof(struct pcb))段错误,应该怎样初始化呢?谢谢
#include<stdlib.h>
struct pcb
{
int pid;
int priority;
struct pcb *next;
};
void create(struct pcb**head)
{
*head=(struct pcb*)malloc(sizeof(struct pcb)); //???
(*head)->next=NULL;
}
void insert(struct pcb**head,struct pcb*stu)
{
struct pcb*curent;
curent=(*head)->next;
if(curent==NULL)
{
curent=stu;
(*head)->next=stu;
curent->next=NULL;
}
while(curent->next!=NULL)
{
curent=curent->next;
}
stu->next=curent->next;
curent->next=stu;
}
void print(struct pcb**head)
{
struct pcb*curent;
curent=*head;
while(curent!=NULL)
{
printf("%d",curent->pid);
curent=curent->next;
}
}
int main()
{
struct pcb**h;
struct pcb*stu1;
struct pcb*stu2;
stu1=(struct pcb*)malloc(sizeof(struct pcb));
stu2=(struct pcb*)malloc(sizeof(struct pcb));
stu1->pid=0;
stu2->pid=1;
create(h);
// free(h);
return 0;
}
编译过程发现*head=(struct pcb*)malloc(sizeof(struct pcb))段错误,应该怎样初始化呢?谢谢