user2799508
user2799508

Reputation: 848

Can we have Macros inside a structure?

This code is taken from Accessing SPI Devices in Linux

static struct spi_board_info
spi_stm32_flash_info__dongle = {
#if defined(CONFIG_SPI_SPIDEV)
.modalias = "spidev",
#endif
.max_speed_hz = 25000000,
.bus_num = 3,
.chip_select = 0,
.controller_data = &spi_stm32_flash_slv__dongle,
};

spi_stm32_flash_info__dongle is atructure?

What is hapenning here? Do we have if else insde structure? Is there any significance of '.; before the variables? I know '.' is used for accesing structure elements, but here there is nothing before '.'

Upvotes: 0

Views: 61

Answers (2)

ugoren
ugoren

Reputation: 16441

  1. Macros are a way to edit the text before compilation, and are processed before the compiler parses the code and figures out stuff like structs. In this case, the modalias line will either be there, or not, when the code is compiled.

  2. The . in this case is used to initialize fields by their names. It's special syntax for this purpose.

Upvotes: 2

cf-
cf-

Reputation: 8866

The contents of the {} are initializing the properties of the struct.

The #ifdef block is entirely legal within the struct, since the compiler will never see it; the preprocessor will either leave the contents of the block in or remove it, depending on the status of CONFIG_SPI_SPIDEV. The Windows API does a similar thing with some structs, adding or removing some members using #ifdef blocks.

Upvotes: 0

Related Questions