控件到达非无效函数的末尾
Control reaches at the end of non-void function
我正在做一个关于Hackerrank的问题,但每当我编译代码时,它都会显示控制到达非void函数的末尾。以下是我的源代码:
/*
Compare two linked lists A and B
Return 1 if they are identical and 0 if they are not.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int CompareLists(Node *headA, Node* headB)
{
if(headA==NULL&&headB==NULL)
{
return 1;
}
else if( headA!=NULL&&headB!=NULL)
{
while(headA!=NULL&&headB!=NULL)
{
if(headA->data==headB->data)
{
headA=headA->next;
headB=headB->next;
}
else
{
return 0;
exit (0);
}
return 1;
}
}
else
{
return 0;
}
}
请告诉我如何纠正这一点,并提前表示感谢。
我可以在这里看到两个可达性问题。首先是简单的一个:
{
return 0;
exit (0);
}
无法访问exit
调用。(那条线几乎肯定是个错误。我想不出有什么好的理由在那里打exit
。(
下一个更复杂的。。。这就是编译错误的根本原因:
while(headA!=NULL&&headB!=NULL)
{
if(headA->data==headB->data)
{
headA=headA->next;
headB=headB->next;
}
else
{
return 0;
exit (0);
}
return 1;
}
看看return 1;
在哪里。它在循环内部。
那么,如果headA != NULL && headB != NULL
的计算结果为false,会发生什么呢?在这种情况下,将跳过末尾为return 1;
的循环体。。。然后你就到了方法的终点。
因此出现了编译错误。
我怀疑"修复"是将return
移到循环之后,但我没有试图理解代码的逻辑,所以这可能是错误的"修复"。
执行此代码后会发生什么?
if(headA->data==headB->data)
{
headA=headA->next;
headB=headB->next;
}
如果是headA->next == NULL
还是headB->next == NULL
?
相关文章:
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- C++无效的函数类型转换
- C++无效使用非静态成员函数?
- 模板和无效函数调用C++
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 无效打印(矢量<string>)函数未打印
- 在 stream_descriptor::async_wait 中无效使用非静态成员函数
- C26486 - 不向函数传递可能无效的指针?
- 为什么构造函数的虚拟函数调用有时有效,但其他调用却无效
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- 使用 vector.sort() 的非静态成员函数无效使用
- 将数组传递给构造函数无效
- 使用auto参数munmap_chunk()返回auto的函数:无效指针
- Boost::函数绑定的成员函数无效
- 为什么在模板类中声明函数无效?
- 使用非英语文件名调用avio_open函数无效
- Android NDK线程使用非静态成员函数无效