harrymc
Reged: Oct 04 2004
Posts: 4
Loc: Perth, Western Australia
|
Sizeof yields 0 (warning)
Thu Apr 17 2008 11:30 PM
|
|
|
Hello
I've struck a little problem porting some code that compiles with SDCC (ANSI C99) to C51 (ANSI C89 ?).
$ c51 -v HI-TECH 8051 C COMPILER V8.00 running on debian etch
The mm.c test: ----------------------- #define USB_DT_STRING 0x3
code unsigned char desc_serial_str[] = { sizeof(desc_serial_str), USB_DT_STRING, '1',0,'2',0,'3',0,'4',0,'5',0,'6',0 };
main() { } ----------------------- gives: mm.c: 4: Sizeof yields 0 (warning)
This compiles and correctly fills element 0 using SDCC; I suspect this is because of variable array size handling of C99.
Examining the mm.hex file shows that element 0 is initialized to zero: 00 03 31 00 32 00 33 00 34 00 35 00 36 00
I'd really like to find a way to have the size calculated by the C51. Some of the arrays are quite large so counting the entries and hard coding the size is a source of (human) error.
I've experimented with a desc_serial_end: label inside the array initialization body and a calculated size: desc_serial_end - desc_serial_str (hoping the linker would resolve the size) but the compiler expects a constant and this fails (as does the use of a label anyway).
Using C allows some flexible definition of USB descriptors based on modules compiled and linked for the target device and it replaces less flexible .asm files.
I'd rather not have a script that builds the descriptors if I can avoid it (so maintainers/developers have an easy view of how the descriptors are created).
Is there a C89 (or C51 specific) solution for this ?
All the best Harry
|
|