Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward declaration in C?

Tags:

c

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    
like image 645
helpdesk Avatar asked Dec 20 '25 18:12

helpdesk


1 Answers

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.

like image 195
MByD Avatar answered Dec 22 '25 07:12

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!