NO

Author Topic: is it possible to make forward TYPE declaration  (Read 4542 times)

whatsup

  • Guest
is it possible to make forward TYPE declaration
« 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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: is it possible to make forward TYPE declaration
« Reply #1 on: October 29, 2010, 07:35:32 AM »
something like this:
Code: [Select]
// forward declaration
typedef struct T2 T2;

typedef struct _tagT1 {
T2 *t2;
} T1;
May the source be with you

whatsup

  • Guest
Re: is it possible to make forward TYPE declaration
« Reply #2 on: October 29, 2010, 03:23:03 PM »
thank you very much, i'll check this

whatsup

  • Guest
Re: is it possible to make forward TYPE declaration
« Reply #3 on: December 09, 2010, 04:06:28 AM »
I tried this, and it didn't work for me.
what i want to do is:

Code: [Select]
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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: is it possible to make forward TYPE declaration
« Reply #4 on: December 09, 2010, 05:38:34 AM »
Code: [Select]
typedef struct _test1 {
  int      num1;
  void   (*PrintNum1)(struct test1 * );  // <-- this is a pointer to the struct
}test1;
May the source be with you

whatsup

  • Guest
Re: is it possible to make forward TYPE declaration
« Reply #5 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)

Code: [Select]
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;
}

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: is it possible to make forward TYPE declaration
« Reply #6 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:
Code: [Select]
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;
}
 
« Last Edit: December 09, 2010, 05:15:36 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

whatsup

  • Guest
Re: is it possible to make forward TYPE declaration
« Reply #7 on: December 09, 2010, 06:15:50 PM »
thank you very much sir.
it works exactly as i wanted.