Question

#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;
    }


}

I am a student learning linked lists and this is a homework project.

The function 'struct car *addtolist(struct car *first)' was originally not a function but was simply enclosed in a for loop, looping 'numcar' times, while I was getting it to work properly, which I did.

For the next step I simply wanted to add the function bits, the declaration and return, and then enclose the call for the function in a for loop.

My problem is that the compiler keeps telling me that the return type does not match the function type.

What did I do wrong?

Was it helpful?

Solution

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

As long as you write these 2 lines inside main(), you declared the function prototype of addtolist(), and started a scope. You are not defining the function addtolist(). Thus, the later return newcar; returns a variable with incorrect type since main() returns int.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top