c-Q:编译器链表错误,但仍在运行



我不确定为什么会出现这些错误,但最奇怪的是,我仍然能够运行程序并产生我想要的结果。

我得到的编译器错误是

IntelliSense:类型为"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;
}

您的结构定义中有一个拼写错误。下一个变量的类型是MyStuct_Struct。在MyStuct_Struct上检查拼写。它仍然有效,因为C显然不是类型安全的。指针就是指针,这样你就不会出现内存错误。

相关内容

  • 没有找到相关文章

最新更新