I have been set a task to read an input using getchar() and then storing this in a list using calloc() to assign the memory space for it.
The thing is each new character needs to be put at the head of the list. I have tried to create some code to do this but am encountering a problem, as I seem to be transferring a parameter between two functions that is a different data type.
I am getting an "incompatible type for argument '1' of insertitem" error from the compiler which I presume points to the first item being different type to that in the function insertitem.
The thing is that in my teaching class I have been taught to construct the functions for inserting into the list and adding new items to the head of a list like this. Which has confused me as they don't seem to work together, Have I been taught wrong?
The relevant parts of code are highlighted.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct L{
int x;
struct L *next;
}list;
typedef struct {
list *head;
list *tail;
} fulllist;
list *insertlist(int hd, list *t1){
list *t = calloc(1,sizeof(list));
t->x = hd;
t->next = t1;
return t;
}
fulllist insertitem(fulllist a, int b){
if (a.head == NULL){
a.head = a.tail = insertlist(b, NULL);
}
else{
a.head = insertlist(b, a.head);
}
return a;
}
list *mainlist(){
list *h = NULL;
int i = getchar();
while (i != '.'){
h = insertitem(h,i);
}
return h;
}
int main(){
list *z = mainlist();
return 0;
}