#define inside a struct?

D

Daniel Rudy

What is the purpose of having a #define inside a struct?

typedef struct {
uByte bLength;
uByte bDescriptorType;
uWord wTotalLength;
uByte bNumInterface;
uByte bConfigurationValue;
uByte iConfiguration;
uByte bmAttributes;
#define UC_BUS_POWERED 0x80
#define UC_SELF_POWERED 0x40
#define UC_REMOTE_WAKEUP 0x20
uByte bMaxPower; /* max current in 2 mA units */
#define UC_POWER_FACTOR 2
} UPACKED usb_config_descriptor_t;

The example is from the usb.h header file on FreeBSD. The compiler is gcc.
 
R

Robert Harris

Daniel said:
What is the purpose of having a #define inside a struct?

typedef struct {
uByte bLength;
uByte bDescriptorType;
uWord wTotalLength;
uByte bNumInterface;
uByte bConfigurationValue;
uByte iConfiguration;
uByte bmAttributes;
#define UC_BUS_POWERED 0x80
#define UC_SELF_POWERED 0x40
#define UC_REMOTE_WAKEUP 0x20
uByte bMaxPower; /* max current in 2 mA units */
#define UC_POWER_FACTOR 2
} UPACKED usb_config_descriptor_t;

The example is from the usb.h header file on FreeBSD. The compiler is gcc.
The #define's aren't syntactically part of the struct. They are
preprocessed out at a stage before the compiler parses the struct
definitions.

Putting them where they are conveys (to a human) that possible values
for the structure members bmAttributes and bMaxPower are those #define'd
below them.

Robert
 
M

Mike Wahler

Daniel Rudy said:
What is the purpose of having a #define inside a struct?

Same purpose as having them anywhere else.
I'll guess that in your example they were put inside
the struct definition because those are probably
values to be used for assigning to one or more
members of that struct (iow, put the information
near where it will be used).

-Mike
 
D

Daniel Rudy

At about the time of 10/16/2005 3:33 AM, Robert Harris stated the following:
The #define's aren't syntactically part of the struct. They are
preprocessed out at a stage before the compiler parses the struct
definitions.

Putting them where they are conveys (to a human) that possible values
for the structure members bmAttributes and bMaxPower are those #define'd
below them.

Robert

I knew that there were pre-processed out, but I didn't understand why
they would be placed in the middle of a struct.

Now I know.

Thanks.
 
A

Alan Balmer

What is the purpose of having a #define inside a struct?

typedef struct {
uByte bLength;
uByte bDescriptorType;
uWord wTotalLength;
uByte bNumInterface;
uByte bConfigurationValue;
uByte iConfiguration;
uByte bmAttributes;
#define UC_BUS_POWERED 0x80
#define UC_SELF_POWERED 0x40
#define UC_REMOTE_WAKEUP 0x20
uByte bMaxPower; /* max current in 2 mA units */
#define UC_POWER_FACTOR 2
} UPACKED usb_config_descriptor_t;

The example is from the usb.h header file on FreeBSD. The compiler is gcc.

Presumably the purpose is to aid the reader by having the definitions
near the point of use. For what it's worth, I wouldn't do it that way,
it interferes with the layout of the struct, imo. I would collect the
defines elsewhere, and document the permissible values as an end of
line comment, e.g.

uByte bmAttributes; /* UC_BUS_POWERED, UC_SELF_POWERED,
or UC_REMOTE_WAKEUP */
 
D

Daniel Rudy

At about the time of 10/17/2005 9:16 AM, Alan Balmer stated the following:
Presumably the purpose is to aid the reader by having the definitions
near the point of use. For what it's worth, I wouldn't do it that way,
it interferes with the layout of the struct, imo. I would collect the
defines elsewhere, and document the permissible values as an end of
line comment, e.g.

uByte bmAttributes; /* UC_BUS_POWERED, UC_SELF_POWERED,
or UC_REMOTE_WAKEUP */

I like your idea better myself. This was found in an offical operating
system header file for application use. This was something that I have
never seen before which lead to my confusion.
 

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,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top