Skip to content

Instantly share code, notes, and snippets.

@ryanmjacobs
Last active April 25, 2018 23:25
Show Gist options
  • Save ryanmjacobs/660c7dc5e858852c2c7b9412731e25ef to your computer and use it in GitHub Desktop.
Save ryanmjacobs/660c7dc5e858852c2c7b9412731e25ef to your computer and use it in GitHub Desktop.
Example of C99 Designator Clauses inside a `const struct`
// temp. includes so that the compiler doesn't complain
// (delete these three lines when you import the code)
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
struct spi_devices_struct {
char manu[10]; // ID winbond 0xef.
char device[10]; // ID string (ie W25Q16BV)
uint8_t manId; // manu ID
uint8_t devId; // device code assigned by Manu
char size[3]; // mega bits
uint8_t tBlocks; // number of blocks in device, 32K block
uint8_t endBlkA23; // address of last block
uint8_t endBlkA15; //
bool supported;
char type; // assign char as mappable ID
};
// C99 Designator Clauses
// http://en.cppreference.com/w/c/language/struct_initialization
const struct spi_devices_struct spiDev[] = {
// Device 0
{ .manId = 0xef,
.devId = 0x14,
.size = "16",
.tBlocks = 64,
.endBlkA23 = 0x1f,
.endBlkA15 = 0x80,
.manu = "Winbond",
.device = "W25Q16BV",
.supported = true,
.type = 0x41 },
// Device 1
{ .manId = 0x1f,
.devId = 0x66,
.size = "1",
.tBlocks = 8,
.endBlkA23 = 0x03,
.endBlkA15 = 0x80,
.manu = "Atmel",
.device = "AT25FS010",
.supported = false,
.type = 0x42 },
// Device 2
{ .manId = 0xef,
.devId = 0x16,
.size = "64",
.tBlocks = 128,
.endBlkA23 = 0x7f,
.endBlkA15 = 0x80,
.manu = "Winbond",
.device = "W25Q64BV",
.supported = true,
.type = 0x43 }
};
int main(void) {
for (int i = 0; i < 3; i++)
printf("spiDev[%d] : %s %s\n", i, spiDev[i].manu, spiDev[i].device);
return 0;
}
@ryanmjacobs
Copy link
Author

$ gcc c99_designators_in_const_struct.c -o test

$ ./test 
spiDev[0] : Winbond W25Q16BV
spiDev[1] : Atmel AT25FS010
spiDev[2] : Winbond W25Q64BV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment