C 返回链表第n个节点的函数的返回类型错误
这是一个返回链表第n个节点的函数,但是编译器经常错误地说返回类型应该是int。这是为什么C 返回链表第n个节点的函数的返回类型错误,c,compiler-errors,C,Compiler Errors,这是一个返回链表第n个节点的函数,但是编译器经常错误地说返回类型应该是int。这是为什么 struct Node *getNthNode(struct Node* head, int index) { if (head==NULL) return NULL; struct Node *current = head; int count = 0; while (current) { if (count == index)
struct Node *getNthNode(struct Node* head, int index)
{
if (head==NULL)
return NULL;
struct Node *current = head;
int count = 0;
while (current)
{
if (count == index)
return(current);
count++;
current = current->next;
}
如果while的条件不再成立,你会返回什么 例如,如果列表中有10个元素,并且传递20作为
索引
参数
如果您没有为这种情况提供return语句,那可能就是问题所在。很可能您在声明函数之前调用了该函数,因此它默认返回int类型。我们必须查看整个文件才能确定。查找所有编译器警告
int main() {
char* p;
p = foo(); // Compiler assumes default int return type
return 0;
}
char* foo() {
}
你展示的片段除了糟糕的缩进之外没有任何明显的错误,但是它是不完整的。请发布一个完整的测试程序,我们可以自己编译并查看问题。如果您发布未编辑的编译器输出,这也会很有帮助。