I am writing a Queue class implementation, and my Queue holds a object called Flight. I tried to create this object by struct. Some part of my code is here:

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

typedef struct {
  int C_TIME;
  int D_TIME;
  char FL_NUM;
  char D_CITY;
#define MAXSIZE 5    
typedef Flight ItemType;
typedef int PosType;
typedef struct{
        ItemType Data[MAXSIZE];
        PosType Rear,Front;

QType Q;

void CreateQ(void);
int EmptyQ(void);
int FullQ(PosType NewR);
void AddQ(ItemType Item);
void RemoveQ(ItemType*);

void CreateQ(void)
     int i;
     for (i=0;i<MAXSIZE-1;i++){
         Q.Data[i]=NULL; // Here it does not allow Q.Data[i].C_TIME.

In the main program I can create object by using "Flight" struct, but in the Queue class it does not allow : Q.Data.D_DATE or Q.Data.C_DATE

Any idea?

9 Years
Discussion Span
Last Post by Duoas

You are making two errors:

The type of Q.Data[n] is a Flight (aka ItemType) --not a pointer. You cannot assign NULL to a struct.

When I tested it, the following compiled correctly: Q.Data[i].C_TIME = 0; (which is what you were complaining about, so I cannot replicate your problem).

Your for loop has a fencepost error. It should be: for (i = 0; i < MAXSIZE; i++) Also, I would recommend against using typedef gratuitously. If ItemType is a Flight, then use "Flight" in your QType structure, instead of renaming it to something else...

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.