-3

这是一个返回链表的第 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)
            return(current);
        count++;
        current = current->next;
    }
4

2 回答 2

2

很可能您在声明函数之前调用了该函数,因此默认返回类型为 int。我们必须查看整个文件才能确定。查找所有编译器警告。

int main() {
    char* p;
    p = foo();   // Compiler assumes default int return type
    return 0;
}

char* foo() {
}
于 2013-03-26T03:52:53.990 回答
0

如果 while 的条件不再为真,你会返回什么?

例如,如果您的列表中有 10 个元素,并且您将 20 作为index参数传递。

如果您没有为这种情况提供返回声明,那可能就是问题所在。

于 2013-03-26T03:49:50.370 回答