cyclic references of types

O

Onno

Hi,

I want to use a pointer to a methode that has a struct (i2cmessage_t) as a
parameter. This struct should have the pointer to the previously defined
methode as one of its members.
The code is below.

typedef void (*i2c_callback) (i2cmessage_t);

typedef struct{
slaveAddress_t slaveAddress;
uint8_t * sendBuffer;
uint8_t sendBufferLength;
uint8_t * receiveBuffer;
uint8_t receiveBufferLength;
i2c_callback callbackFunction;
} i2cmessage_t;

when I compile this code, gcc (actualy avr-gcc) generates a warning:
"parameter names (without types) in function declaration"
Obviously, this is not going to work.

How do I declare both types and have them refer to eachother. Is it
posible to write a forward declaration of a type or something?

Thanks in advanced.
grz,
Onno
 
H

Hallvard B Furuseth

Onno writes:

Onno said:
I want to use a pointer to a methode that has a struct (i2cmessage_t)
as a parameter. This struct should have the pointer to the previously
defined methode as one of its members.

'struct foo;' predeclares a struct. But you can't do the same with
typedefs. So,

Prepend:

typedef struct i2cmessage_s i2cmessage_t;
typedef void (*i2c_callback) (i2cmessage_t);

typedef struct{ ... } i2cmessage_t;

and replace that with:

struct i2cmessage_s { ... };

Or use:

struct i2cmessage_s;
typedef void (*i2c_callback) (struct i2cmessage_s);
typedef struct i2cmessage_s { ... } i2cmessage_t;
 
C

Chris Torek

I want to use a pointer to a method that has a struct (i2cmessage_t) as a
parameter. This struct should have the pointer to the previously defined
methode as one of its members.

(C does not have anything formally called "methods", but your intent
is clear enough.)
typedef void (*i2c_callback) (i2cmessage_t);

typedef struct{
slaveAddress_t slaveAddress;
uint8_t * sendBuffer;
uint8_t sendBufferLength;
uint8_t * receiveBuffer;
uint8_t receiveBufferLength;
i2c_callback callbackFunction;
} i2cmessage_t;

See <http://web.torek.net/torek/c/types2.html>, in particular the
section on self- and mutually-referential types.
 
O

Onno

I want to use a pointer to a methode that has a struct (i2cmessage_t)
struct i2cmessage_s;
typedef void (*i2c_callback) (struct i2cmessage_s); typedef struct
i2cmessage_s { ... } i2cmessage_t;

Thanks,
I got it working nicely.

grz,
Onno
 
B

Bill Pursell

Onno said:
Thanks,
I got it working nicely.

You might note the following: (taken from the info
page for libc):

* Names that end with `_t' are reserved for additional type names.
 
K

Keith Thompson

Bill Pursell said:
You might note the following: (taken from the info
page for libc):

* Names that end with `_t' are reserved for additional type names.

The C standard doesn't reserve names ending in "_t". Is that a POSIX
thing?
 
R

Richard Heathfield

Keith Thompson said:
The C standard doesn't reserve names ending in "_t". Is that a POSIX
thing?

Yes. It's useful knowledge, even though - strictly speaking - it's nothing
to do with C. One would not wish to tread on POSIX's namespace toes by
mistake. (To do so deliberately would be another matter; if one chooses to
do so, presumably one has a good reason.)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top