[英]Function Doesn't return
struct m_queue{
int ndata;
struct m_queue* pnext;
struct m_queue* pdown;
};
int search(struct m_queue* list,int key){ //returns the index where it founded the key, return -1 if key is not found
struct m_queue* temp;//searches horizontal
struct m_queue* run;//searches downward
int i;
temp = list;
run = temp->pdown;
getch();
while(temp!=NULL){
getch();
while(run!=NULL){
if(run->ndata == key)
return temp->ndata;
else
run = run->pdown;
}
temp = temp->pnext;
run = temp->pdown;
}
printf("returning -1");
getch();
return -1;
}
i checked the other functions already and i'm pretty sure there's no flaw but whenever i debug my program it always highlight the segment "run = temp->pdown". 我已经检查了其他功能,并且我很确定没有缺陷,但是每当我调试程序时,它总是突出显示段“ run = temp-> pdown”。 this functions doesn't return -1 it just return if it found the key.
此函数不返回-1,而是在找到键后才返回。 this was coded in c language
这是用C语言编码的
If temp
is NULL
, run = temp->pdown;
如果
temp
为NULL
,则run = temp->pdown;
is an invalid operation. 是无效的操作。
}
temp = temp->pnext;
if (temp!=NULL) //add this line
run = temp->pdown;
The problem lies in these lines: 问题出在以下几行:
temp = temp->pnext;
run = temp->pdown;
what happens when temp
is NULL? 当
temp
为NULL时会发生什么? You are trying to access temp->pdown
without checking whether temp
is NULL. 您尝试访问
temp->pdown
而不检查temp
是否为NULL。
//run = temp->pdown; //this statement not needed
getch();
while(temp!=NULL){
run = temp->pdown; //add this statement
getch();
while(run!=NULL){
if(run->ndata == key)
return temp->ndata;
else
run = run->pdown;
}
temp = temp->pnext;
// run = temp->pdown; // this statement not needed
}
make this change to avoid accessing temp when it is NULL. 进行此更改以避免在为NULL时访问temp。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.