Conflict types in function

1

I'm trying to create a list structure using struct .

But when I compile my code, I get an error because, even specifying Lista , it has a problem.

Follow the complete code:

#include <stdio.h>
#include <conio.h>

struct lista {
    int info;
    struct lista* prox;
};

typedef struct lista Lista;

int main(void)
{
    Lista* l; 
    l = list_new();
    l = list_add(l, 23);
    l = list_add(l, 45);
    list_print ( l ); 

    return(0);
}

Lista* list_new (void)
{
    return NULL;
}

Lista* list_add (Lista* l, int i)
{
    Lista* novo = (Lista*) malloc(sizeof(Lista));
    novo -> info = i;
    novo -> prox = l;
    return novo;
}

void list_print (Lista* l)
{
    do {
        printf(“%d\t”,l->info);
        l = l->prox;
    } while (l != NULL); 
}

The error is giving in line 22:

  

[Error] conflicting types for 'list_new':

     

List * list_new (void)

    
asked by anonymous 16.08.2016 / 19:52

2 answers

2

I put the code in the ideone and after solving a series of other problems not listed in the question the code compiled and ran perfectly.

If you want to make it more explicit and perhaps meet some compiler requirement you are using, cast cast on the returned type to match the return type. So:

#include <stdio.h>
#include <stdlib.h>

struct lista {
    int info;
    struct lista* prox;
};

typedef struct lista Lista;

Lista* list_new(void) {
    return (Lista*)NULL;
}

Lista* list_add(Lista* l, int i) {
    Lista* novo = malloc(sizeof(Lista));
    novo -> info = i;
    novo -> prox = l;
    return novo;
}

void list_print(Lista* l) {
    do {
        printf("%d\t",l->info);
        l = l->prox;
    } while (l != NULL); 
}

int main(void) {
    Lista* l = list_new();
    l = list_add(l, 23);
    l = list_add(l, 45);
    list_print(l); 
    return 0;
}

See running on ideone .

    
16.08.2016 / 20:02
1

Simple, you say that the list_new function returns Lista and then is returning NULL :

Lista* list_new (void)
{
    return NULL;
}
    
16.08.2016 / 19:57