问:链接列表的编译器错误,但仍然运行

时间:2015-12-11 02:12:05

标签: c pointers struct malloc

我不确定为什么我会收到这些错误,但最奇怪的是,我仍然可以运行该程序并生成我想要的结果。

我得到的编译错误是

  

智能感知:类型" MyStruct *"不能分配给类型" Mystuct_struct *"的实体。

有4个这样的实例,但正如我在标题中所述,程序似乎运行正常,并按我的意愿显示bin文件。是的,我知道我的结构名称很糟糕。

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


typedef struct MyStruct_struct

{
  char FlightNum[7];
  char OriginAirportCode[5];
  char DestAirportCode[5];
  int timeStamp;
  struct Mystruct_struct* next;
} MyStruct;

int main()
{
  time_t time;
  MyStruct * ptr;
  MyStruct *  head; 
  MyStruct * tail;
  MyStruct * temp;
  FILE * bin;
  MyStruct myStruct;

  bin = fopen("acars.bin", "rb");
  ptr= (struct MyStruct_struct *) malloc (sizeof(MyStruct) );    
  fread(ptr,sizeof(MyStruct)-sizeof(MyStruct*),1,bin); 

  head = ptr; // make head point to that struct
  tail = ptr; // make tail point to that struct

  while (1) 
    {
      ptr = (struct MyStruct_struct *) malloc(sizeof(MyStruct)); 
      fread(ptr, sizeof(MyStruct) - sizeof(MyStruct*), 1, bin); 
      tail->next = ptr; // error here
      tail = tail->next; //error here

      if (feof(bin) != 0)
        break;




    }
  tail->next = NULL;

  ptr = head;
  while (ptr->next != NULL) 
    {

      printf("%s ", ptr->FlightNum);
      printf("%s ", ptr->OriginAirportCode);
      printf("%s ", ptr->DestAirportCode);
      time = ptr->timeStamp;
      printf("%s",ctime( &time));
      ptr = ptr->next; // here
    }

  ptr = head;
  while (ptr->next != NULL) 
    {

      temp = ptr;

      ptr = ptr->next; //error here
      free(temp);
    }


  fclose(bin);
  system("pause");
  return 0;
}

1 个答案:

答案 0 :(得分:2)

您的结构定义中有拼写错误。接下来是变量的类型是MyStuct_Struct。检查MyStuct_Struct上的拼写。它仍然有效,因为C很奇怪,不是类型安全的。指针是指针是一个指针,所以你不会得到内存错误。