XavierPaul
XavierPaul

Reputation: 1

incomplete definition of type struct & forward declaration of 'struct ElementoDiLista

When I try to compile this small snippet of code I get the following errors:

./main.c:25:8: error: incomplete definition of type 'struct ElementoDiLista'
  lista->info=10;
  ~~~~~^
./main.c:12:16: note: forward declaration of 'struct ElementoDiLista'
typedef struct ElementoDiLista* ListaDiElementi;

advice please ....

the code:

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

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

typedef struct elemento ElementoDiLista;
typedef struct ElementoDiLista* ListaDiElementi;


int main(void) {
  
  
  ElementoDiLista elem;
  elem.info=10;
  elem.next=NULL;
  ListaDiElementi lista;
  
  
  lista=malloc(sizeof(ElementoDiLista));
  lista->info=10;
  
 
  
  return 0;
}

I expect that my code works since come from a book.

Upvotes: 0

Views: 857

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

You do not have complete type struct ElementoDiLista. You have complete type struct elemento and its alias ElementoDiLista.

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

typedef struct elemento ElementoDiLista;

So instead of this typedef definition

typedef struct ElementoDiLista* ListaDiElementi;

you have to write

typedef ElementoDiLista* ListaDiElementi;

The compiler issues an error because in this typedef definition

typedef struct ElementoDiLista* ListaDiElementi;

you introduced a new type specifier struct ElementoDiLista that is an incomplete type and has nothing common with struct elemento nor with its alias ElementoDiLista.

Upvotes: 2

Related Questions