Re: Forward Reference for Typedef'd Type?

Discussion in 'C Programming' started by SM Ryan, May 19, 2006.

  1. SM Ryan

    SM Ryan Guest

    Michael B Allen <> wrote:
    # I have a forward reference like:
    #
    # struct foo;
    #
    # int some_fn(struct foo *param);
    #
    # Because the parameter is a pointer the compiler is satisfied.
    #
    # But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
    # following all fail to compile:
    #
    # foo_t;
    # typedef foo_t;
    # typedef struct foo_t;
    #
    # Is there any way to declare a forward refernce for a tyepdef'd type?

    You can use
    typdef struct foo foo_t;
    and then subsequently
    struct foo {
    type field; ...
    };

    If you're only using a pointer to a struct, you don't have to define
    the struct: all struct pointers look the same. This means you can
    possibly arrange your declarations as

    typedef struct A A;
    typedef struct B B;
    typedef struct C C;
    ...
    other declarations using (A*), (B*), and (C*);
    ...
    struct A {
    type field;...
    A *a; B *b; C *c;
    };
    struct B {
    type field;...
    A *a; B *b; C *c;
    };
    struct C {
    type field;...
    A *a; B *b; C *c;
    };
    ...
    declarations and functions that use the struct fields
    ...

    You can possibly divide this into a header and compile file like

    something.h
    typedef struct A A;
    typedef struct B B;
    typedef struct C C;
    ...
    other declarations using (A*), (B*), and (C*);
    ...
    something.c
    #include "something.h"
    struct A {
    type field;...
    A *a; B *b; C *c;
    };
    struct B {
    type field;...
    A *a; B *b; C *c;
    };
    struct C {
    type field;...
    A *a; B *b; C *c;
    };
    ...
    declarations and functions that use the struct fields
    ...


    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    One of the drawbacks of being a martyr is that you have to die.
     
    SM Ryan, May 19, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    634
    Jonathan Turkanis
    Feb 15, 2004
  2. Jordi Vilar
    Replies:
    5
    Views:
    2,308
    Jonathan Turkanis
    Feb 18, 2004
  3. Plok Plokowitsch

    forward declaration vs. typedef

    Plok Plokowitsch, May 30, 2005, in forum: C++
    Replies:
    2
    Views:
    13,400
    Donovan Rebbechi
    May 30, 2005
  4. Jordi Vilar
    Replies:
    5
    Views:
    18,010
    Jonathan Turkanis
    Feb 18, 2004
  5. Michael B Allen

    Forward Reference for Typedef'd Type?

    Michael B Allen, May 18, 2006, in forum: C Programming
    Replies:
    7
    Views:
    400
    Michael B Allen
    May 19, 2006
Loading...

Share This Page