Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The size of an initialized array as element of the array (USB descriptor)

Tags:

c++

arrays

sizeof

I want to have a construct similar to this (invalid) one:

const uint8_t uArray[] = { uint8_t(sizeof(uArray)), 1, 2, 3 }; 

and the uArray[0] should be 4 and be filled automatically at the compile time, this is the situation for USB descriptors definition that are usually expressed as an array of bytes with the first byte value being the size in elements.

This theoretically should be possible because the array is initialized already but somehow I'm not able to express it.

Any suggestions to solve this are welcomed.

like image 308
Kik the Cat Avatar asked Sep 27 '22 20:09

Kik the Cat


1 Answers

I read the specification http://open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf, and in paragraph 6.7.8.19 it states that the initialisation happens in initialisation list order, and in 6.7.8.22 it says "If an array of unknown size is initialized, its size is determined by the largest indexed element with an explicit initializer. At the end of its initializer list, the array no longer has incomplete type." To me, this suggests that at the time of initialisation of the first value, the type of the array (and its size) are still incomplete.

like image 190
Kif Avatar answered Nov 15 '22 10:11

Kif