[英]Linked List In Data Structures
假設temp
是指向node
的指針。 temp->next
為NULL
。 那么temp->next->next
的值是多少?
簡而言之, NULL->next
的值是什么? 是否因為我在ubuntu和代碼塊(windows)中看到了不同的結果而依賴於編譯器?
下面的程序輸出是什么?
struct node
{
int data;
struct node *next;
};
main()
{
struct node *temp,*p;
int c=0;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=50;
temp->next=NULL;
p=temp;
if(p->next->next==NULL)//will it enter the if loop?
c++;
printf("%d",c);
}
如果temp->next
為NULL,則對其進行解引用以獲取temp->next->next
是undefined behavior 。 可能會發生崩潰,但是可能會發生其他事情。 原則上, 任何事情都可能發生。
不要取消引用空指針。
NULL-> next必須給您一個段錯誤。
您可能想擁有類似的東西:
if(p->next != NULL && p->next->next==NULL)
要么
if(p->next == NULL || p->next->next==NULL)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.