简体   繁体   English

在列表中使用struct时,“取消引用不完整类型的指针”-C

[英]“Dereferencing pointer to incomplete type” when using struct in list - C

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 ". 我是数据结构的新手,并试图制作一个程序,将数据从.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;
}

I'm using Code::Blocks. 我正在使用Code :: Blocks。 What is wrong with my code? 我的代码有什么问题?

inic in main should be of type anuro or struct pessoa , not struct anuro . inic main应该是anuro struct pessoa而不是 struct anuro struct anuro doesn't exist in your code. 您的代码中不存在struct anuro Declaring inic like 像这样声明inic

aluno *inic;

should fix the problem. 应该解决问题。


Notes: 笔记:

  • you pass arguments of type anuro s to the functions. 您将类型anuro s的参数传递给函数。 Remove the * when calling the functions to actually pass anuro* s, ie pointers 在调用函数以实际传递anuro* *时删除* ,即指针

  • 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) 缺少显式的main声明,其返回类型为int 仅适用于C99之前的代码 (当未指定返回类型时,返回类型默认为int

  • you call fscanf with the format specifier "%s" twice but pass a char ( e[50] / f[50] ). 您用格式说明符"%s"调用fscanf两次,但传递了一个chare[50] / f[50] )。 It's undefined behavior. 这是未定义的行为。 Furthermore, both subscripts are out of bounds (the last element in both is [49] ); 此外,两个下标都超出范围(两个下一个元素均为[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 您可能打算只传递数组的地址,而可以通过将ef传递给fscanf来完成

  • don't cast the return value of malloc 不要转换malloc的返回值

buscamatricula(*inic, a);  //also an error here

Function int buscamatricula(aluno *i, int m) requires an pointer to struct . 函数int buscamatricula(aluno *i, int m)需要一个pointer to structpointer 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. cad给出的答案可以解决您的错误。

When you call this function insere, do you want the data to get into inic that you just created ? 当您将此函数调用为insere时,是否要使数据进入刚创建的inic中? Because that's not what the insere() function is doing, please confirm. 因为那不是insere()函数的作用,所以请确认。

   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 * 我要修改结构,记得将结构地址发送给接收函数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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM