Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place #define in structure

Tags:

c

From linux kernel code

struct gpio_desc {
    struct gpio_chip    *chip;
    unsigned long       flags;
/* flag symbols are bit numbers */
#define FLAG_REQUESTED  0
#define FLAG_IS_OUT 1
#define FLAG_EXPORT 2   /* protected by sysfs_lock */
#define FLAG_SYSFS  3   /* exported via /sys/class/gpio/control */
#define FLAG_ACTIVE_LOW 6   /* value has active low */
#define FLAG_OPEN_DRAIN 7   /* Gpio is open drain type */
#define FLAG_OPEN_SOURCE 8  /* Gpio is open source type */
#define FLAG_USED_AS_IRQ 9  /* GPIO is connected to an IRQ */
#define FLAG_IS_HOGGED  11  /* GPIO is hogged */

    /* Connection label */
    const char      *label;
    /* Name of the GPIO */
    const char      *name;
};

what is the reason to place defines into the body of structure?

like image 217
artsin Avatar asked Oct 21 '16 16:10

artsin


2 Answers

With #define it doesn't matter too much where you put them (so long as it is higher up in the file than where it is first used). Most likely those constants are used only within that structure, so it was put there so logically they would be easier to find. They could have been put anywhere above the first place they were used, but they were grouped together because of similar purpose.

like image 90
Cody Avatar answered Oct 08 '22 03:10

Cody


It's meaningless, other than to try and define them close to the point of first use.

By the time the compiler actually sees the code, the preprocessor will have stripped those lines out of it.

In this particular example, they would be better off with a typedef'd enum for the flags and using that enum to declare the field.

like image 2
kdopen Avatar answered Oct 08 '22 03:10

kdopen