问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果
int main()
{
SLinkNode* L = NULL;
InputElem_Node(L);
}
void InputElem_Node(SLinkNode* L) {
int n = 0;
SLinkNode* p1, * p2;
p1 = p2 = (SLinkNode*)malloc(sizeof(SLinkNode));
printf("please input the first elem:");
scanf("%d", &p1->data);
while (p1->data != 0) {
n++;
if (n == 1) L = p1;
else p2->next = p1;
p2 = p1;
p1 = (SLinkNode*)malloc(sizeof(SLinkNode));
printf("please input the next elem:");
scanf("%d", &p1->data);
}
p2->next = NULL;
}
想问下各位,上述的址传递,不是直接可以对定义的结构体地址进行操作吗,感觉这样的跟下面的返回结构体指针类型的函数差不多啊,但就是想不通是哪里没考虑到
int main()
{
SLinkNode* L;
L = Input_Node();
}
SLinkNode* Input_Node(void) {
int n = 0;
SLinkNode* L;
SLinkNode* p1, * p2;
p1 = p2 = (SLinkNode*)malloc(sizeof(SLinkNode));
printf("please input the first elem:");
scanf("%d", &p1->data);
L = NULL;
while (p1->data != 0) {
n++;
if (n == 1) L = p1;
else p2->next = p1;
p2 = p1;
p1 = (SLinkNode*)malloc(sizeof(SLinkNode));
printf("please input the next elem:");
scanf("%d", &p1->data);
}
p2->next = NULL;
return L;
}