Conflict of types in function

Asked

Viewed 497 times

1

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

Only when compiling my code, I get an error because even specifying the type Lista, he’s in some kind of trouble.

Follow the full 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 on line 22:

[Error] Conflicting types for 'list_new':

List* list_new (void)

  • Do you know that you can also vote on everything on the site, in addition to accepting an answer in your questions? Take a look at [tour].

2 answers

3


I put on the ideone. And in the repl it.. Also put on the Github for future reference. And after solving a number of other problems not listed in the question the code compiled and ran perfectly.

If you want to make it more explicit and maybe meet some compiler requirement you’re using, do a cast type returned 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); 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • That’s right. That’s right.

  • Wow, I’m following what was in the college brochure. So it’s all wrong.... I’m used to higher-level languages. Thanks for the help!

  • 1

    @Mariodem.Barrosneto Yes, starting by using conio which is obsolete and non-standard and which was not even necessary in this case, wrong use of the malloc() organisation of duties, lack of a include necessary, not to mention minor style inconsistencies. Regrettably, the workbooks spread around, even in faculties, are quite wrong and we must be very careful. I did not notice if there are errors of logical or other "invisible".

1

Simple, you say the function list_new returns of type Lista and then it’s returning NULL:

Lista* list_new (void)
{
    return NULL;
}
  • But I’m returning a pointer from List, and in this case, a pointer could not be NULL?

  • Cast the NULL cast.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.