Why is this error? - invalid application of ?sizeof' to incomplete type

Asked

Viewed 1,098 times

2

When I try to compile(gcc test.c) it generates the following error:

The code:

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

typedef struct aluno Aluno;

int main()
{
    Aluno *a;
    a=malloc(sizeof(Aluno));

    //Insere dados nos campos do tipo Aluno
    a->matricula=123;
    strcpy(a->nome,"victhor");
    strcpy(a->curso,"computação");

    int *matricula;
    char *nome, *curso;

    //Copia os dados de Aluno para as variáveis
    matricula=(int*)&(a->matricula);
    nome=(char*)&a->nome;
    curso=(char*)&a->curso;

    acessa(a,matricula, nome, curso);
    printf("Matrícula: %d\n",*matricula);
    printf("Nome: %s\n", nome);
    printf("Curso: %s\n", curso);
    return 0;
}

typedef struct aluno{
    int matricula;
    char nome[50];
    char curso[20];
}Aluno;
  • You tried to define the struct at the beginning before the main()? If so, what appears?

1 answer

2

Put the following code before the function main.

typedef struct aluno{
    int matricula;
    char nome[50];
    char curso[20];
}Aluno;

Reason: He’s doing the malloc before loading the data structure itself, and so is giving error.

  • I already did that and it really works out the problem is that this is just a chunk of code. I will create a file where the struct and functions to manipulate it will be in a separate file from the code in which they will be used will be linked by a header.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.