![](/img/trans.png)
[英]dereferencing pointer to incomplete type error when using typedef struct in C
[英]“Dereferencing pointer to incomplete type” when using struct in list - C
我是数据结构的新手,并试图制作一个程序,将数据从.txt读取到结构pessoa中 ,将其传输到列表中,然后获取所需的结构,但我不断收到此错误:“ 错误:取消指向不完整类型 “”的指针 。
#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;
}
我正在使用Code :: Blocks。 我的代码有什么问题?
inic
main
应该是anuro
型或 struct pessoa
, 而不是 struct anuro
。 您的代码中不存在struct anuro
。 像这样声明inic
aluno *inic;
应该解决问题。
笔记:
您将类型anuro
s的参数传递给函数。 在调用函数以实际传递anuro*
*
时删除*
,即指针
缺少显式的main
声明,其返回类型为int
仅适用于C99之前的代码 (当未指定返回类型时,返回类型默认为int
)
您用格式说明符"%s"
调用fscanf
两次,但传递了一个char
( e[50]
/ f[50]
)。 这是未定义的行为。 此外,两个下标都超出范围(两个下一个元素均为[49]
); 再次出现未定义的行为。 您可能打算只传递数组的地址,而可以通过将e
和f
传递给fscanf
来完成
buscamatricula(*inic, a); //also an error here
函数int buscamatricula(aluno *i, int m)
需要一个pointer to struct
的pointer to struct
。 所以这样叫-
buscamatricula(inic, a);
同样,此调用不正确-
insere(*inic, a, e, f, b); //error here
像这样做 -
insere(inic, a, e, f, b);
当您将此函数调用为insere时,是否要使数据进入刚创建的inic中? 因为那不是insere()函数的作用,所以请确认。
insere(inic, a, e, f, b); //error here
您的问题似乎是对结构,如何传递和修改的混淆。 这是您对程序进行的修改,但尚未完成,我为您留下了一些工作。 我要修改结构,记得将结构地址发送给接收函数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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.