Dillon Wreek
Dillon Wreek

Reputation: 73

calling recursively a function to create a list in C

I want to create a function that takes a list (even if NULL) and adds an element on top of the head. I tried to do the following code, but I get a compilation error:

expected expression before ‘ListaDiElementi’
   crea(&ListaDiElementi lista);

my code is the following:

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

struct elemento
{
    int info;
    struct element* next;
};

typedef struct element ElementOfList;
typedef ElementOfList * ListOfElements;

typedef ListOfElements crea (ListOfElements list)
{
    ListOfElements new = malloc (sizeof(ElementOfList));
    scanf("%d", &new->info);
    new->next=list;
    return new;
}
int main()
{
    ListOfElements list = NULL;
    do
    {
        crea(&ListOfElements list);
        printf("%d", list->info);
    }
    while(list->info>0);
}

I know this feels like "please do my code" but I am really stuck and I don't have any idea on how to do this.

Upvotes: 1

Views: 209

Answers (1)

Sumeet
Sumeet

Reputation: 8292

You are not supposed to pass the type in function call and also list is already a pointer so dont use &: So instead of

crea(&ListOfElements list);

use

crea(list);

Also typedef cannot be used in return type so the function signature should be:

ListOfElements crea (ListOfElements list){.....}

Upvotes: 1

Related Questions