Reputation: 1497
I was reading bootloader code of some OS and came up with such syntaxt:
pde_t entry_pgdir[NPDENTRIES] = {
// Map VA's [0, 4MB) to PA's [0, 4MB)
[0]
= ((uintptr_t)entry_pgtable - KERNBASE) + PTE_P,
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
[KERNBASE>>PDXSHIFT]
= ((uintptr_t)entry_pgtable - KERNBASE) + PTE_P + PTE_W
};
What does it mean — [0] = …
?
Upvotes: 2
Views: 7849
Reputation: 726669
This is C99 array initialization syntax (aka ‘designated initializer’). It lets you mix regular values, e.g.
pde_t entry_pgdir[NPDENTRIES] = {val1, val2}
with [ind1] = val1, [ind2] = val2
syntax. The semantics of this is that the value in square brackets is interpreted as an index, and the value after =
is interpreted as the value to store at the corresponding index.
The remaining entries are zeroed out. If you put regular values after an indexed one, the numbering continues at index+1
.
This syntax is convenient when the data has gaps, for example
int logval[] = {
[1<<0] = 1
, [1<<1] = 2
, [1<<2] = 3
, [1<<3] = 4
...
};
This is easier to read than its plain equivalent:
int logval[] = {0, 1, 2, 0, 3, 0, 0, 0, 4, ...}
Upvotes: 4
Reputation: 12432
It is called a "designated initializer". It means that the following initializer is for the first element of the array (index 0). In this case, it is redundant, because the first initializer would be for the first element anyway. It is here to provide parallelism with the second initializer, which has [KERNBASE>>PDXSHIFT]
as the designator.
The initializer as a whole provides an initial value for 2 of the array elements. The rest of the array is initialized to 0.
Upvotes: 1
Reputation: 170104
what does it mean? [0] = smth?
It's a designated initializer. A C99 construct that allows you to specify the array element which will be initialized with smth
. So one does not have to rely on the position of the initializer to match that of the array element implicitly. Instead the initialization is made explicit (and IMO that's better).
Upvotes: 2