473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Forward Reference for Typedef'd Type?

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

May 18 '06 #1
7 15234
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/
May 18 '06 #2
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

May 18 '06 #3


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;

--
Er*********@sun.com

May 18 '06 #4
Michael B Allen <mb*****@ioplex.com> 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) ks***@mib.org <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.
May 18 '06 #5
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 '06 #6

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

May 19 '06 #7
On Thu, 18 May 2006 21:48:51 +0000, Keith Thompson wrote:
Michael B Allen <mb*****@ioplex.com> 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

May 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
5
by: aaragon | last post by:
Hello, I was wondering if it is possible to forward declare a type definition. If so, what is the way to do it? I found a couple of messages where they say it's not possible but there must be a way...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.