0

I'm new to Data Structure, and tried to make a program that reads data from a .txt to a struct pessoa, transfer it to a list, and then gets the desired struct back, but I keep getting this error: "error: dereferencing pointer to incomplete type".

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

typedef struct pessoa{
int matr;
char *nome;
char *curso;
int semestre;
struct pessoa *prox;
}aluno;

int insere(aluno *i, int m, char *n, char *c, int s){
aluno *h = (aluno*)malloc(sizeof(aluno));
h->matr=m;
h->nome=n;
h->curso=c;
h->semestre=s;
h->prox=i;
i=h;
return 0;
}

int buscamatricula(aluno *i, int m){
    char n;
    char c;
    int s;
    while (i->prox != NULL){
        if (m == i->matr)
        {
            printf("Nome: %s\nMatricula: %i\nCurso: %s Semestre: %i\n", n, m, c, s);
            break;
        }
    }
    puts("Erro: nao foi possivel encontrar o aluno");
return 0;
}

main()
{
int x=0, a, b;
char e[50], f[50];
struct aluno *inic;
FILE *arq;
arq = fopen("turma.txt", "r");
if (arq == NULL)
    puts("Deu ruim");
else
{
        while (fscanf(arq, "%i %s %s %i", &a, e[50], f[50], &b) != EOF)
    {
        insere(*inic, a, e, f, b); //error here
    }
    fclose(arq);
}
while (x != -255)
{
    printf("Que matricula vc deseja pesquisar? Para sair, digite \"-255\"\n");
    scanf("%i", &x);
    if (x == -255)
        break;
    buscamatricula(*inic, a);  //also an error here
}
free(inic);
return 0;
}

I'm using Code::Blocks. What is wrong with my code?

0

4 Answers 4

2

inic in main should be of type anuro or struct pessoa, not struct anuro. struct anuro doesn't exist in your code. Declaring inic like

aluno *inic;

should fix the problem.


Notes:

  • you pass arguments of type anuros to the functions. Remove the * when calling the functions to actually pass anuro*s, i.e. pointers

  • lack of an explicit declaration of main with return type int works only for pre-C99 code (return type defaults to int when none is specified)

  • you call fscanf with the format specifier "%s" twice but pass a char (e[50]/f[50]). It's undefined behavior. Furthermore, both subscripts are out of bounds (the last element in both is [49]); undefined behavior again. You probably meant to pass just the addresses of the arrays, what you can accomplish by passing e and fto fscanf instead

  • don't cast the return value of malloc

Sign up to request clarification or add additional context in comments.

9 Comments

@ameyCU No, in her/his code it's struct anuro *inic;. Is that what you mean?
@ameyCU, no it isn't. inic is declared struct aluno *. That references an otherwise undeclared struct with tag aluno. aluno is the typedef-ed name of struct pessoa.
@cad Ohh , didn't notice that . Sorry for that .
@cad Thanks, there are no more error when I build it. But I still cannot understand one thing: it seems that my code never reaches the end of file. Can you please explain me why?
@LeandroOliveiraRezende I added a point in the "Notes" section of my answer. Does this fix your problem?
|
2
buscamatricula(*inic, a);  //also an error here

Function int buscamatricula(aluno *i, int m) requires an pointer to struct . So call like this -

buscamatricula(inic, a);  

Similarly , this call is incorrect -

insere(*inic, a, e, f, b); //error here

Do it like this -

insere(inic, a, e, f, b); 

The answer given by cad here address your error.

Comments

0

When you call this function insere, do you want the data to get into inic that you just created ? Because that's not what the insere() function is doing, please confirm.

   insere(inic, a, e, f, b); //error here

1 Comment

Yes, that's what I want to do. How can I fix that?
0

Your problem seems to be a confusion with structures, how to pass and modify. Here is your program modified BUT it is not finished, I left some work for you. I you want to modify the struct remember to send struct address & to receiving function star *

 #include <usual.h>


  struct pessoa

 {

 int matr;
 char *nome;
 char *curso;
 int semestre;
 struct pessoa *prox;
 };

 typedef struct pessoa aluno;

 int insere( aluno * h, int m, char *n, char *c, int s )
 {
 //aluno *h = (aluno*)malloc(sizeof(aluno));
  h->matr = m;
  h->nome = n;
  h->curso = c;
  h->semestre = s;
 //h->prox=i;
 //i=h;
 return 0;
  }

  int buscamatricula( aluno i, int m )
 {
  char n;
  char c;
  int s;
  while ( i.prox != NULL )
  {
   if ( m == i.matr )
   {
    printf( "Nome: %s\nMatricula: %i\nCurso: %s Semestre: %i\n", n, m, c,
      s );
   break;
   }
  }
    puts( "Erro: nao foi possivel encontrar o aluno" );
    return 0;
 }

 int main(  )
{
 int x = 0, a, b;
 char e[50], f[50];
 //struct aluno *inic;
  aluno inic;

  FILE *arq;
  arq = fopen( "turma.txt", "r" );
  if ( arq == NULL )
  puts( "Deu ruim" );
  else
  {
   while ( fscanf( arq, "%i %s %s %i", &a, e[50], f[50], &b ) != EOF )
   {
    insere( &inic, a, e, f, b );    //error was here
   }
   fclose( arq );
  }
 while ( x != -255 )
 {
  printf
   ( "Que matricula vc deseja pesquisar? Para sair, digite \"-255\"\n" );
  scanf( "%i", &x );
 if ( x == -255 )
   break;
 buscamatricula( inic, a ); //also an error here
}
//free(inic);  I didn't malloc so cannot free it
  return 0;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.