struct stu **p;
creat(p);
这里传的p值,不是传引用或者地址,当然p的值就不会变,是个未知值,所以(*p)->num肯定会报错
改成如下,把p的地址传过去
struct stu *p;
creat(&p);
struct stu **p;
creat(p);
printf("%d",p->num);
return 0;
#include
#include
#include
#include
struct stu
{
int num;
struct stu *next;
};
void creat (struct stu **&head)
{
head=(struct stu **)malloc(sizeof(struct stu));
*head=new struct stu;
if(!*head)
{
printf("分配内存失败!");
return ;
}
else
{
(*head)->num=5;
(*head)->next=NULL;
}
}
int main()
{
struct stu **p=NULL;
creat(p);
printf("%d",(*p)->num);
return 0;
}