C Question

Push on linked lists - C

Hello I'm dooing a program in c with linked lists and I don't know what i`m doing wrong. The problem is on the line: "podio novo = (struct podio) malloc(sizeof(podio));" inside of push.
here is my code:

struct Sucesso {
char nome_equipe[N1];
float distancia_alvo;
float tempo_propulsao;

struct Node {
struct Sucesso *dados;
struct Node *prox;

typedef struct Node podio;

void push(podio *p, struct Sucesso elem) {
podio *novo = (struct podio*) malloc(sizeof(podio));
if(novo != NULL) {

Answer Source

The problem is that struct podio is an incomplete type unrelated to the type podio (aka struct Node) defined via typedef struct Node podio;. Therefore, the two types (podio * and struct podio *) are incompatible; they point to different types of object.

If you lose the struct in the malloc() line, your code should compile.

podio *novo = (podio *) malloc(sizeof(podio));


podio *novo = malloc(sizeof(*novo));

There'll be those who castigate you for casting the result of malloc(); fortunately for you, I'm not one of them.