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?
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 anuro
s to the functions. Remove the *
when calling the functions to actually pass anuro*
s, ie 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 f
to fscanf
instead
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);
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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.