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;
}Flight;
    
#define MAXSIZE 5    
typedef Flight ItemType;
typedef int PosType;
typedef struct{
        ItemType Data[MAXSIZE];
        PosType Rear,Front;
        }QType;

QType Q;

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

void CreateQ(void)
{     
     int i;
     Q.Front=0;
     Q.Rear=0;
     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?

Recommended Answers

All 3 Replies

You are making two errors:

1.
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).

2.
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.

What?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.