Forward Reference for Typedef'd Type?

M

Michael B Allen

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
 
M

Marc Thrun

Michael said:
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.
 
M

Michael B Allen

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
 
E

Eric Sosman

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;
 
K

Keith Thompson

Michael B Allen said:
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.
 
S

swengineer001

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.
 
N

Nelu

Michael said:
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.
 
M

Michael B Allen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top