-5

What does this structure represent? The first square bracket [last_ds_type] shows the array of the structure. What about inner square bracket like [ds_1307]? What is the . in .nvram_offset and .nvram_size?

static struct chip_desc chips[last_ds_type] = {
  [ds_1307] = {
    .nvram_offset  = 8,
    .nvram_size  = 56,
  },
  [ds_1308] = {
    .nvram_offset  = 8,
    .nvram_size  = 56,
  },
  [ds_1337] = {
    .alarm    = 1,
    .century_reg  = DS1307_REG_MONTH,
    .century_bit  = DS1337_BIT_CENTURY,
  },
  [ds_1338] = {
    .nvram_offset  = 8,
    .nvram_size  = 56,
  },
  [ds_1339] = {
    .alarm    = 1,
    .century_reg  = DS1307_REG_MONTH,
    .century_bit  = DS1337_BIT_CENTURY,
    .trickle_charger_reg = 0x10,
    .do_trickle_setup = &do_trickle_setup_ds1339,
  },
  [ds_1340] = {
    .century_reg  = DS1307_REG_HOUR,
    .century_enable_bit = DS1340_BIT_CENTURY_EN,
    .century_bit  = DS1340_BIT_CENTURY,
    .trickle_charger_reg = 0x08,
  },
  [ds_1388] = {
    .trickle_charger_reg = 0x0a,
  },
  [ds_3231] = {
    .alarm    = 1,
    .century_reg  = DS1307_REG_MONTH,
    .century_bit  = DS1337_BIT_CENTURY,
  },
  [rx_8130] = {
    .alarm    = 1,
    /* this is battery backed SRAM */
    .nvram_offset  = 0x20,
    .nvram_size  = 4,  /* 32bit (4 word x 8 bit) */
  },
  [mcp794xx] = {
    .alarm    = 1,
    /* this is battery backed SRAM */
    .nvram_offset  = 0x20,
    .nvram_size  = 0x40,
  }
};
Dennis Vennink
  • 1,083
  • 1
  • 7
  • 23
rajan
  • 1
  • 2
  • That's designated initializers. – HolyBlackCat Nov 09 '17 at 13:51
  • 1
    Although this might have come from Linux device-driver code, only the actual identifiers within are specific to that context. In any case, what you're looking at are designated initializers. They specify which element of the array is being initialized at each point, and which member of each (`struct chip_desc`) array element. – John Bollinger Nov 09 '17 at 13:52
  • 1
    @HolyBlackCat: How odd - two questions on the C tag on designated initialisers in the last hour; never seen one before. – Bathsheba Nov 09 '17 at 13:53

3 Answers3

2

This array of structs is using the array designated initialiser syntax to initialise specific elements in the array to a certain value.

Each token such as ds_1307 is most likely a macro evaluating to an integer.

The .member notation is another flavour of designated initialiser, which the programmer is using to initialise the members of a particular element of the array. Members that are not set explicitly are set to the same value as they would be if the instance had static storage duration.

See also what is a designated initializer in c?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1
[ds_1307] = {
    .nvram_offset   = 8,
    .nvram_size = 56,
}

means element number ds_1307 which most likely is a macro like this:

#define ds_1307             1

in your array of structs to be initialized with nvram_offset = 8 and nvram_size = 56

Simpler example:

char array[2] = { [0] = 'a', [1] = 'b' };

is equivalent to

array[0] = 'a'
array[1] = 'b'    
Martin Chekurov
  • 733
  • 4
  • 15
0

Here chips[last_ds_type] is array of structure of type chip_desc. This code is a part of RTC driver , you can visit this link https://github.com/rushup/Kitra710-kernel/blob/master/drivers/rtc/rtc-ds1307.c Here you will find a RTC driver code, where things you wants to know are explained.

chunky
  • 75
  • 1
  • 2
  • 11