|
发表于 2020-8-10 11:53:19
|
显示全部楼层
本帖最后由 xieglt 于 2020-8-10 11:54 编辑
参数传错了,要传指针的指针
int InitList(LNode * *L)
{
* L = (LNode *)malloc(sizeof(LNode));
if(*L == NULL)
return 0;
(*L)->next = NULL;
return 1;
}
int main(void)
{
LNode *Lnode;
InitList(&Lnode);
if(Lnode != NULL)
{
Lnode->data = 99;
printf("%d\n",Lnode->data);
}
//注意释放内存
free(Lnode);
return 0;
}
或者写成返回值
LNode * InitList(void)
{
LNode * L = (LNode *)malloc(sizeof(LNode));
if(L != NULL)
{
L->next = NULL;
}
return L;
}
nt main(void)
{
LNode *Lnode = InitList();
if(Lnode != NULL)
{
Lnode->data = 99;
printf("%d\n",Lnode->data);
}
//注意释放内存
free(Lnode);
return 0;
}
|
|