gpt4 book ai didi

c - Q : Compiler Error of linked list, 但仍在运行

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:50 27 4
gpt4 key购买 nike

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

我得到的编译器错误是

IntelliSense: a value of type "MyStruct *" cannot be assigned to an entity of type "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 不是类型安全的。指针是指针是指针,因此您不会遇到内存错误。

关于c - Q : Compiler Error of linked list, 但仍在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com