Patrick Daniels
Patrick Daniels

Reputation: 51

Q: Compiler Error of linked list, but still runs

I am not sure why I am getting these errors, but the weirdest thing, I am still able to run the program and produce the results I want.

The compiler error I get is

IntelliSense: a value of type "MyStruct *" cannot be assigned to an entity of type "Mystuct_struct *"

There are 4 instances of this, but as I stated in my title, the program still seems to run fine, and displays the bin file as I want it to. And yes, I know the name of my structure is bad.

#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;
}

Upvotes: 0

Views: 60

Answers (1)

RD3
RD3

Reputation: 1089

You have a typo in your struct definition. The type for your variable next is MyStuct_Struct. Check your spelling on MyStuct_Struct. It still works because C is joiously not type safe. A pointer is a pointer is a pointer so you don't end up with memory errors.

Upvotes: 2

Related Questions