Pelles C forum

C language => Beginner questions => Topic started by: whatsup on October 29, 2010, 02:11:21 AM

Title: is it possible to make forward TYPE declaration
Post by: whatsup on October 29, 2010, 02:11:21 AM
i have a struct (T1)
inside the struct (T1) i have function pointer which get a struct (T2) pointer as an argument
the problem is that struct T2 doesn't declared yet,
and i get compiler error.

thanks in advanced for any help
Title: Re: is it possible to make forward TYPE declaration
Post by: TimoVJL on October 29, 2010, 07:35:32 AM
something like this:
// forward declaration
typedef struct T2 T2;

typedef struct _tagT1 {
T2 *t2;
} T1;
Title: Re: is it possible to make forward TYPE declaration
Post by: whatsup on October 29, 2010, 03:23:03 PM
thank you very much, i'll check this
Title: Re: is it possible to make forward TYPE declaration
Post by: whatsup on December 09, 2010, 04:06:28 AM
I tried this, and it didn't work for me.
what i want to do is:


typedef struct _test1 {
  int      num1;
  void   (*PrintNum1)(test1 * );  // <-- this is a pointer to the struct
}test1;


i get the error: Extraneous old-style parameter list.
Title: Re: is it possible to make forward TYPE declaration
Post by: TimoVJL on December 09, 2010, 05:38:34 AM
typedef struct _test1 {
  int      num1;
  void   (*PrintNum1)(struct test1 * );  // <-- this is a pointer to the struct
}test1;
Title: Re: is it possible to make forward TYPE declaration
Post by: whatsup on December 09, 2010, 03:55:25 PM
thanks for reply.
this also doesn't work.
here is my code (i mentioned in the first post i'm using a function pointer)


typedef struct _test1 {
  void   (*PrintNum1)(struct test1 *);
}test1;


void    test1__PrintNum1 (struct test1 *self);

void    test1__PrintNum1 (struct test1 *self)
{
}

void init(void)
{
struct test1 t;
t.PrintNum1 = test1__PrintNum1;
}
Title: Re: is it possible to make forward TYPE declaration
Post by: frankie on December 09, 2010, 05:11:49 PM
In forward reference you have to refer to the tag (_test1) not to the type defined (test1) that must be used in all successive instances:
typedef struct _test1 {
 void   (*PrintNum1)(struct _test1 *);
}test1;


void    test1__PrintNum1 (test1 *self);

void    test1__PrintNum1 (test1 *self)
{
}

void init(void)
{
test1 t;
t.PrintNum1 = test1__PrintNum1;
}
Title: Re: is it possible to make forward TYPE declaration
Post by: whatsup on December 09, 2010, 06:15:50 PM
thank you very much sir.
it works exactly as i wanted.