An array of character shall hold a Pascal string. This array is part of a struct and shall be initialized with an expression at compile time. This is possible in that way:
struct V {
char version[2];
char pstringa[20];
char pstringb[30];
};
struct V variable = {
{ 1, 0 },
{ 3, 'A', 'B', 'C' },
{ "\3""XYZ" }
};
Now the strings in the initialization list are generated and the length can vary. Therefore it is desired to have a preprocessor macro that creates the initializer.
There is a possibility with MSVC to use { 3, "ABC" }
as initializer of the member pstringa
. This could be used in a macro:
#define PASCAL_STRGING(s) sizeof(s)-1,s
struct V variable = {
{ 1, 0 },
{ 3, 'A', 'B', 'C' },
PASCAL_STRGING("XYZ")
};
This is unfortunately not compliant with the C89 standard. Is there a way to create the form "\3""XYZ"
from the macro parameter "XYZ"? The number behind the backslash shall be the number of characters in the string.
I know that it would be possible to create a struct with the length byte of the Pascal string and the remaining characters. But this shall be avoided because the structs are defined in external header files.
Edit
Some annotations initially missing in the question:
- The compiler is for an embedded device (AVR-ATmega).
- The structure shall be filled at compile time, because the structure is stored in flash memory.