3

I have this ordered List structure that has a structure that has two members, an array of type Titem and an int counter. Now, this List can take any type and arrange it in ascending order. Suppose, i decided to typedef char Titem, then the array contains characters, if i typedef int Titem, then the array contains integers. Now, I have a structure somewhere with the type Tage;

how do i make the Ordered List identify it. when i did typedef Tage Titem, it complains. Where should I insert it in the OList header file? Or is there a way to do forward declarations like it is done in C++ in C?

 #ifndef OLIST_H
 #define OLIST_H



       /*typedef char Titem; here, i typedef char to Titem, though commented out..
       how do i do similar thing for the Tage datatype i have?

       */


       #define MAX 10
       typedef struct  {
   int count;
   Titem array[MAX]; //Titem is not typedefed yet, so error..
    } TOrderedList;



  void initialize_list(TOrderedList *list);
  int insert_item(TOrderedList *list, Titem item);
  int retrieve_ith(const TOrderedList *list, int i, Titem *item);
  int number_of_items(const TOrderedList *list);
  int list_empty(const TOrderedList *list);

  #endif    
2
  • "when i did typedef Tage Titem, it complains." You should explain "complains". Do you get a warning? error? what is the message? what line?
    – abelenky
    Feb 23, 2012 at 16:57
  • here is the Treg header file, how do i just import this header into the source code above? typedef struct { short int reg; }Treg; void initializeTime(Treg *t);
    – helpdesk
    Feb 23, 2012 at 21:30

3 Answers 3

2

You cannot use forward declaration here, unless you want to use pointers to Titem, like:

#define MAX 10
typedef struct  {
   int count;
   Titem *array[MAX]; //Titem is not typedefed yet, so error..
} TOrderedList;

Since the compiler needs to know the size of Titem in order to create the TOrderedList struct.

Include the definition of Titem before you use it, I wouldn't count on the order of the include files in other files.

1

Or is there a way to do forward declarations like it is done in C++ in C?

No, You cannot use Forward declaration here.

Rationale for why Forward declaration will not work:

When you use forward declaration of any type, the compiler does not know the composition of it nor the members inside it, the compiler only knows that the type exists. Thus, it is an Incomplete type for the compiler. With Incomplete types , One cannot create objects of it or do anything which needs the compiler to know the layout of the type or more than the fact that it is just an type. Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to types as a pointer.
However, here the compiler needs to know the layout and size of the type Titem since it needs to know how much memory to allocate while creating an array, hence forward declaring the type Titem will not work.

Solution:

You need to let the compiler know of the layout of Titem and the way to do that is to include the header defining the type Titem in the source file where you create the array,

1
  • please, how do I include the Titem type in the source file? can you include it in the code somewhere? I don't want to declare the Tage structure. I just want to do something like typedef Treg Titem and expect the souce code above to identify it and link it to the array in the TOrderedList structure.
    – helpdesk
    Feb 23, 2012 at 21:24
0

As you are creating an array of Titem, the compiler needs the full definition of the type; a forward declaration will not suffice.

So you need to fully define Tage before you create your typedef.

1
  • ,Is there no other way i could just #include the Tage in the code above without defining it fully? the Treg has itz own header and source code. how do I just import them into the source code above without redefining it again?
    – helpdesk
    Feb 23, 2012 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.