#include <stdio.h>
typedef int element_type;
typedef struct Cell{
element_type e;
struct Cell *next;
} Cell,*List;
Cell *End(List L){
Cell *q = L;
while (q->next != NULL){
q = q->next;
}
return q;
}
void Insert(Cell *p, element_type x){
//Create new Cell
Cell *temp = (Cell*) malloc(sizeof(Cell));
if (temp == NULL){
printf("Memory Allocation Failed");
}
else{
temp->e = x;
temp->next = p->next;
p->next = temp;
}
}
element_type Retrieve(Cell *p){
return p->e;
}
int main(){
//Initialize the List;
List L = malloc(sizeof(Cell));
L->e = NULL;
L->next = NULL;
element_type x = 10;
Insert(L,x);
printf("Just retrievd the item %d\n",Retrieve(L));
return 1;
}
List_pointer.c: In function ‘Insert’:
List_pointer.c:19:24: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
List_pointer.c: In function ‘main’:
List_pointer.c:35:12: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
List_pointer.c:36:8: warning: assignment makes integer from pointer without a cast [enabled by default]
Thanks for all your helps, and I for the part with struct now. However, when I tried to use malloc, I got the warnings again on incompatible declaration. I thought malloc returns a generic pointer to NULL, and hence there shouldn’t be any issue with casting? I just not sure what I did wrong here.
Also for those of you who wonders why I would implement such a weird interface, I was following the interfaces provided by the book “Data Structure and Algorithm” by Aho. The sample codes were given in Pascal, pretty old style. Although I think there are some merits to learn this super old style way of designing data structure.
Update:
I just forgot to include the stdlib.h header for malloc!
Please refer to this link incompatible implicit declaration of built-in function ‘malloc’
You’ll need to change
to
The
typedef struct { /* ... */ } Cell;defines a tagless struct. In effect, the struct itself has no name via which it can be referred to directly. The nameCellis simply the name of atypedefthat refers to this unnamed struct.When you use
struct Cellto declarenext, it means “the struct namedCell.” But, there isn’t a struct namedCell, because the struct you defined has no name.By naming the struct (giving it a tag), you can refer to it using the
struct Cellnotation.