0

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.
harper
  • 13,345
  • 8
  • 56
  • 105

1 Answers1

2

Hacky approach: overwrite:

#define PASCAL_STRING(f, s)     f = " " s, f[0] = sizeof(s)-1

struct V variable2 = {
    { 1, 0 },
    { 3, 'A', 'B', 'C'  },
    PASCAL_STRING(.pstringb, "XYZ")
};

Caveat emptor. I get warning: initialized field overwritten [-Woverride-init]

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Unfortunately this is not accepted by the compiler. No `.member` is allowed. Even the comma in `, [0} = ...` is treated as error. I fear I need a way to build a string similar to the pstringb (with "\3", 3 is placeholder for the actual length) – harper Jul 05 '23 at 14:09
  • @harper The reason for the still unanswered [question](https://stackoverflow.com/questions/76618119/initialize-char-array-with-a-pascal-string/76618679?noredirect=1#comment135086125_76618119) is to deal with things like [not accepted by the compiler](https://stackoverflow.com/questions/76618119/initialize-char-array-with-a-pascal-string/76618679?noredirect=1#comment135090780_76618679). Its OK to not address these comments, yet it makes progress slower. – chux - Reinstate Monica Jul 05 '23 at 14:39
  • @Reinstate Monica, you are right. That statement was very unspecific. To be more precise, the CodeVisionAVR compiler for AVR microcontroller doesn't support member initializers. With this background I fear that there is no way to implement the desired PASCAL_STRING macro. I will have to stick with counting each string's count if characters r use a kind of script that creates appropriate include files. – harper Jul 29 '23 at 06:03