簡體   English   中英

在列表中使用struct時,“取消引用不完整類型的指針”-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兩次,但傳遞了一個chare[50] / f[50] )。 這是未定義的行為。 此外,兩個下標都超出范圍(兩個下一個元素均為[49] ); 再次出現未定義的行為。 您可能打算只傳遞數組的地址,而可以通過將ef傳遞給fscanf來完成

  • 不要轉換malloc的返回值

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

函數int buscamatricula(aluno *i, int m)需要一個pointer to structpointer to struct 所以這樣叫-

buscamatricula(inic, a);  

同樣,此調用不正確-

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

像這樣做 -

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

cad給出的答案可以解決您的錯誤。

當您將此函數調用為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM