zsljulius
zsljulius

Reputation: 4103

warning incompatible pointer, why this happens?

#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’

Upvotes: 3

Views: 585

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

You need to provide the tag for your struct, not only a typedef:

typedef struct Cell {
    element_type e;
    struct Cell *next; 
} Cell,*List;

Without the tag there, the struct Cell * is undefined, causing the error.

It is very helpful to understand the anatomy of this typedef: it is a combination of two declarations:

struct Cell {
    element_type e;
    struct Cell *next; 
};

and

typedef struct Cell Cell;

Without the tag, you are typedef-ing a tagless struct.

Upvotes: 2

James McNellis
James McNellis

Reputation: 355009

You'll need to change

typedef struct {

to

typedef struct Cell {

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 name Cell is simply the name of a typedef that refers to this unnamed struct.

When you use struct Cell to declare next, it means "the struct named Cell." But, there isn't a struct named Cell, because the struct you defined has no name.

By naming the struct (giving it a tag), you can refer to it using the struct Cell notation.

Upvotes: 3

Related Questions