#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
void insert_last(struct node **head, int value)
{
struct node* new = malloc(sizeof(struct node));
new->data = value;
if( !*head )
{
new->link = NULL;
*head = new;
}
else
{
struct node* last = *head;
while(last->link)
{
last = last->link;
}
new->link = NULL;
last->link = new;
}
}
struct node *head;
int main()
{
insert_last( & head, 5);
insert_last( & head, 10);
insert_last( & head, 15);
printf("%d ", head->data);
printf("%d ", head->link->data);
printf("%d ", head->link->link->data);
}
如果我在 main 中宣告 struct node *head ,則程式無法正常作業。是什么原因 ?> 如果我在全球范圍內宣告它的作業,否則不作業。> 我重復這個問題是因為 stackoverflow 要求添加更多細節(> 如果我在 main 的一側宣告 struct node *head 程式不作業。原因是什么?> 如果我全域宣告它的作業,否則不作業。)
uj5u.com熱心網友回復:
有一個初始化錯誤:在 insert_last() 中,變數 head 在沒有明確初始化的情況下進行了測驗。如果 head 被宣告為 global,則它位于最有可能被加載程式初始化為 0 的全域部分(當程式啟動時);如果 head 在函式 main() 中宣告,則它位于函式的堆疊中并且不設定為 0。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370436.html
上一篇:包含分配指標的結構向量無法銷毀
下一篇:何時使用雙指標和指標
