using struct type in other struct declaration

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

using struct type in other struct declaration

 
0
  #1
Dec 30th, 2007
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5. int C_TIME;
  6. int D_TIME;
  7. char FL_NUM;
  8. char D_CITY;
  9. }Flight;
  10.  
  11. #define MAXSIZE 5
  12. typedef Flight ItemType;
  13. typedef int PosType;
  14. typedef struct{
  15. ItemType Data[MAXSIZE];
  16. PosType Rear,Front;
  17. }QType;
  18.  
  19. QType Q;
  20.  
  21. void CreateQ(void);
  22. int EmptyQ(void);
  23. int FullQ(PosType NewR);
  24. void AddQ(ItemType Item);
  25. void RemoveQ(ItemType*);
  26.  
  27. void CreateQ(void)
  28. {
  29. int i;
  30. Q.Front=0;
  31. Q.Rear=0;
  32. for (i=0;i<MAXSIZE-1;i++){
  33. Q.Data[i]=NULL; [COLOR="Red"]// Here it does not allow Q.Data[i].C_TIME.[/COLOR]
  34. }
  35. }
In the main program I can create object by using "Flight" struct, but in the Queue class it does not allow : Q.Data[i].D_DATE or Q.Data[i].C_DATE

Any idea?
Last edited by Ancient Dragon; Dec 30th, 2007 at 7:00 pm. Reason: start using code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: using struct type in other struct declaration

 
0
  #2
Dec 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

Re: using struct type in other struct declaration

 
0
  #3
Dec 30th, 2007
st add "&& c
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: using struct type in other struct declaration

 
0
  #4
Dec 30th, 2007
What?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC