Forward Reference for Typedef'd Type?

Discussion in 'C Programming' started by Michael B Allen, May 18, 2006.

  1. 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?

    Thanks,
    Mike
     
    Michael B Allen, May 18, 2006
    #1
    1. Advertising

  2. Michael B Allen

    Marc Thrun Guest

    Michael B Allen schrieb:
    > 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?
    >
    > Thanks,
    > Mike
    >


    You have to use the full type identifier, i.e.

    typedef struct foo foo_t;

    in your case. This will create an alias for struct foo called foo_t.

    --
    Marc Thrun
    http://www.tekwarrior.de/
     
    Marc Thrun, May 18, 2006
    #2
    1. Advertising

  3. On Thu, 18 May 2006 23:43:06 +0200, Marc Thrun wrote:

    > Michael B Allen schrieb:
    >> 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?
    >>
    >> Thanks,
    >> Mike
    >>

    >
    > You have to use the full type identifier, i.e.
    >
    > typedef struct foo foo_t;
    >
    > in your case. This will create an alias for struct foo called foo_t.


    But unlike a forward reference this is a type definition. And because I
    need it in multiple header files I will start to get redefinition errors.

    I'm toast.

    Mike
     
    Michael B Allen, May 18, 2006
    #3
  4. Michael B Allen

    Eric Sosman Guest

    Michael B Allen wrote On 05/18/06 17:26,:
    > 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?


    Your problem isn't the forwardness of the reference,
    but that you haven't written the typedef properly. Try

    typedef struct foo foo_t;

    --
     
    Eric Sosman, May 18, 2006
    #4
  5. Michael B Allen <> writes:
    > 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?


    Marc Thrun already answered your question, but I have a question for you:

    Why do you want to do this? Your type already has a perfectly good
    name, "struct foo". What do you want another name for the same thing?

    If your answer is "to save typing", that's not a good reason.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 18, 2006
    #5
  6. Michael B Allen

    Guest


    > > Michael B Allen schrieb:
    > >> 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?
    > >>
    > >> Thanks,
    > >> Mike
    > >>

    > >
    > > You have to use the full type identifier, i.e.
    > >
    > > typedef struct foo foo_t;
    > >
    > > in your case. This will create an alias for struct foo called foo_t.

    >
    > But unlike a forward reference this is a type definition. And because I
    > need it in multiple header files I will start to get redefinition errors.
    >

    sounds like you really want this structure in a seperate header
    included by the others and properly protected, something like a
    common_types.h.
     
    , May 19, 2006
    #6
  7. Michael B Allen

    Nelu Guest

    Michael B Allen wrote:
    > On Thu, 18 May 2006 23:43:06 +0200, Marc Thrun wrote:
    >
    > > Michael B Allen schrieb:


    > >><snip>


    > >> foo_t;
    > >> typedef foo_t;
    > >> typedef struct foo_t;


    > > <snip>

    > But unlike a forward reference this is a type definition. And because I
    > need it in multiple header files I will start to get redefinition errors.
    >
    > I'm toast.


    Why do you need it in multiple header files? Even if the files are part
    of separate projects and you find out that you need to use two file
    headers in the same project, you can use #ifndef in the header files.
    The problem will appear when the structure is different from file to
    file while retaining the same name.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, May 19, 2006
    #7
  8. On Thu, 18 May 2006 21:48:51 +0000, Keith Thompson wrote:

    > Michael B Allen <> writes:
    >> 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?

    >
    > Marc Thrun already answered your question, but I have a question for you:
    >
    > Why do you want to do this? Your type already has a perfectly good
    > name, "struct foo". What do you want another name for the same thing?
    >
    > If your answer is "to save typing", that's not a good reason.


    Actually that's not why. In fact I don't like typedefs. They hide far too
    much. Is it a pointer? Is it a structure or an integer? Bah.

    The reason I need a typedef here is because I'm dynamically generating
    code and the first parameter of the generated functions is a 'user
    defined type' specified to the generator. Currently I'm using a standard
    struct type:

    struct user_defined_type;
    int dynamic_fn(struct user_defined_type *p1);

    But frequently an 'existing type' unknown to the generated code would
    be suitable to use directly as the 'user defined type'. Therefore I
    would like it to be a typedef so that the user can define it as the
    'existing type' using a simple typedef:

    typedef struct existing_type user_defined_type_t;

    and then somehow forward reference the 'user defined type':

    typedef user_defined_type_t; /* forward reference */
    int dynamic_fn(user_defined_type_t *p1);

    I suppose the answer is to either pass the 'existing type' to the
    generator or embed it in another struct that can be forward referenced.

    Mike
     
    Michael B Allen, May 19, 2006
    #8
    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:
    619
    Jonathan Turkanis
    Feb 15, 2004
  2. Jordi Vilar
    Replies:
    5
    Views:
    2,294
    Jonathan Turkanis
    Feb 18, 2004
  3. Plok Plokowitsch

    forward declaration vs. typedef

    Plok Plokowitsch, May 30, 2005, in forum: C++
    Replies:
    2
    Views:
    13,330
    Donovan Rebbechi
    May 30, 2005
  4. Jordi Vilar
    Replies:
    5
    Views:
    17,923
    Jonathan Turkanis
    Feb 18, 2004
  5. SM Ryan

    Re: Forward Reference for Typedef'd Type?

    SM Ryan, May 19, 2006, in forum: C Programming
    Replies:
    0
    Views:
    502
    SM Ryan
    May 19, 2006
Loading...

Share This Page