Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Forward Reference for Typedef'd Type?

Reply
Thread Tools

Forward Reference for Typedef'd Type?

 
 
Michael B Allen
Guest
Posts: n/a
 
      05-18-2006
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

 
Reply With Quote
 
 
 
 
Marc Thrun
Guest
Posts: n/a
 
      05-18-2006
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/
 
Reply With Quote
 
 
 
 
Michael B Allen
Guest
Posts: n/a
 
      05-18-2006
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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-18-2006


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;

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-18-2006
Michael B Allen <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
swengineer001@gmail.com
Guest
Posts: n/a
 
      05-19-2006

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

 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      05-19-2006

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

 
Reply With Quote
 
Michael B Allen
Guest
Posts: n/a
 
      05-19-2006
On Thu, 18 May 2006 21:48:51 +0000, Keith Thompson wrote:

> Michael B Allen <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Forward Reference for Typedef'd Type? SM Ryan C Programming 0 05-19-2006 10:29 AM
Forward reference mess Razvan Java 8 10-04-2004 07:23 AM
Forward class reference in Java? Ken Java 4 07-04-2004 09:33 PM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
forward reference to a class... Francesco C++ 5 09-16-2003 11:55 AM



Advertisments