| | |
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:
Solved Threads: 0
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:
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?
c Syntax (Toggle Plain Text)
#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; [COLOR="Red"]// Here it does not allow Q.Data[i].C_TIME.[/COLOR] } }
Any idea?
Last edited by Ancient Dragon; Dec 30th, 2007 at 7:00 pm. Reason: start using code tags
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:
(which is what you were complaining about, so I cannot replicate your problem).
2.
Your for loop has a fencepost error. It should be:
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.
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.
![]() |
Similar Threads
- put a struct type val in a FILE (C)
- Array in struct (C#)
- C--Structures(inside structure declaration of variable of same type is not allowed) (C)
- Help with copying an array into a struct (C)
- Having trouble trying to use a global structure (C)
- Enum, struct, and pointer? (C)
- Run-time Error when printing Array Contents. (C)
- i don't know how to start (C++)
- new member here :) (C)
- Structure Help Please (C++)
Other Threads in the C Forum
- Previous Thread: my program text file do not display properly. Please help...
- Next Thread: SDES in C
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






