473,387 Members | 1,597 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,387 software developers and data experts.

#define inside a struct?


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.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Nov 15 '05 #1
5 13446
Daniel Rudy wrote:
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
Nov 15 '05 #2

"Daniel Rudy" <sp******@spamthis.net> wrote in message
news:I2*****************@newssvr21.news.prodigy.co m...

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

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.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Nov 15 '05 #3
At about the time of 10/16/2005 3:33 AM, Robert Harris stated the following:
Daniel Rudy wrote:
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


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

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Nov 15 '05 #4
On Sun, 16 Oct 2005 09:28:08 GMT, Daniel Rudy <sp******@spamthis.net>
wrote:

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 */

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #5
At about the time of 10/17/2005 9:16 AM, Alan Balmer stated the following:
On Sun, 16 Oct 2005 09:28:08 GMT, Daniel Rudy <sp******@spamthis.net>
wrote:

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 */


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.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Nov 15 '05 #6

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

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
0
by: ooze | last post by:
typedef unsigned long PARAM; typedef PARAM SAP; typedef struct qnode { struct qnode *next; struct qnode *prev; } QNODE;
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
1
by: Jón Sveinsson | last post by:
Hello everone I have code from c which I need to convert to c#, the c code is following, as you can see there is a struct inside struct typedef struct { some variables....
12
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
4
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.