返回值类型与函数类型c编程链表不匹配

时间:2014-05-02 02:20:26

标签: c linked-list return-value

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxchar 50

int numcars; 
int c;//counter
char fu[5];//debug
int main()
{
    typedef struct car //info of car
    {
        int year;
        char vin[maxchar];
        char make[maxchar];
        char model[maxchar];
        car *next;
    };

    car *first;
    car *newcar;


    printf("Enter the Number of Cars you have: ");
    scanf("%i", &numcars);

    struct car *addtolist(struct car *first);
    {
        newcar = (car*)malloc(sizeof(car));

        printf("\nEnter The Model Year: ");
        scanf("%i", &newcar->year);
        printf(" ");//for some reason the program always skips the first 'gets' so this one is here to take that hit.
        gets(fu);
        printf("\nEnter The Make: ");
        gets(newcar->make);
        printf("\nEnter The Model: ");
        gets(newcar->model);
        printf("\nEnter The Vehicle Identifaction Number: ");
        gets(newcar->vin);

        newcar->next = first;
        first = newcar;
        return newcar;
    }


}

我是学生学习链表,这是一个家庭作业项目。

功能&#39; struct car * addtolist(struct car * first)&#39;最初不是一个功能,而是简单地用 for循环,循环&#39; numcar &#39;时间,虽然我让它正常工作,我做了。

下一步我只想添加功能位,声明返回,然后在 for循环中包含对该函数的调用

我的问题是编译器一直告诉我返回类型与函数类型不匹配。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

struct car *addtolist(struct car *first);
{

只要在 main()内写下这两行,就会声明addtolist()的函数原型,并启动一个范围。您没有定义函数addtolist()。因此,后来的return newcar;会返回一个类型不正确的变量,因为main()会返回int