This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

String literals with "character codes" > 0xff

I'd like to initialize string constants such that the high byte of each character can be nonzero.

The string constants are pointed to by members of a struct as in the following code:

struct foo_struct {
  char *s;
};

const struct foo_struct foo = { "\x2021\x2223" };

When compiling this code I get the warning:

"test.c", line 182: warning #27-D: character value is out of range
  const struct foo_struct foo = { "\x2021\x2223" };

And the corresponding location in memory is initialized with 0x21, 0x23, 0x00.

I could achieve what I want by doing something like this:

struct bar_struct {
  const unsigned *u;
};

const unsigned u_ar[] = {0x2021, 0x2223, 0};

const struct bar_struct bar = { u_ar };

What I don't like with this approach is the requirement of the intermediate array u_ar. I don't need that, since I only access the data using the pointers in the structures.

I'd appreciate any hints on how to initialize string constants such that both bytes of a character can be used (I want to save some memory).

BTW, is there a more recent User's Guide to the compiler than spru514c.pdf? spru514c.pdf is marked preliminary and for version 5.0.0 of the compiler. I'm using version 5.2.3 now.

 

Thanks in advance

Johannes

 

  • Hi Johannes,

    Not sure if it helps but, One option could be to use "malloc" instead of constant character array.

    After assigning the particular value to your structure variable, you can free the memory using free()

    This way you just use this memory temporarily, and as a final outcome you also save memory.

    Hope this helps.

    Regards,

    Sid

  • The fact that the parser won't accept 16-bit hex escapes for 16-bit char appears to be a bug.

    As a workaround, try:

    struct one {
    const char *s;
    };

    const char data[2] = { 0x2021, 0x2223 };
    const struct one thing = { data };

    or:

    struct another {
    const char[2];
    };

    const struct another thing = { { 0x2021, 0x2223 } };
  • Archaeologist said:

    The fact that the parser won't accept 16-bit hex escapes for 16-bit char appears to be a bug.

    I'm not sure whether I could expect the compiler to accept 16-bit hex escapes but it would be quite handy if it did. Do you know whether there is a more recent version of the compiler manual than spru514c.pdf?  Should I report this as a bug? Where would I report this?

    Archaeologist said:

    As a workaround, try:

    struct one {
    const char *s;
    };

    const char data[2] = { 0x2021, 0x2223 };
    const struct one thing = { data };

    That's similar to what I wrote in my original posting and what I didn't like because of the need of the array variable (data[] in your example). In terms of "saved memory" it is what I want (except for the fact that I'd define data as const char data[] = { 0x2021, 0x2223, 0}; in order to be able to find the end of the data).


    Archaeologist said:
    
    

    or:

    struct another {
    const char[2];
    };

    const struct another thing = { { 0x2021, 0x2223 } };

    This would require to declare "struct another" such that the longest possible string would fit into the array. In my application I have quite a lot of objects of type "struct another" and the string constants have different lengths.  That's why I'd prefer to have the struct member point to a null terminated string.

    Thank you anyway for your reply

    Regards

    Johannes

  • johannes said:
    I'm not sure whether I could expect the compiler to accept 16-bit hex escapes but it would be quite handy if it did. [..] Should I report this as a bug? Where would I report this?

    I've checked the standard, and it clearly expects this to be legal, so this is definitely a bug.  I've submitted this as SDSCM00034277.  Use this number to track the progress of a fix.

    johannes said:
    Do you know whether there is a more recent version of the compiler manual than spru514c.pdf? 

    Sorry, it seems that version C is the very latest.

    johannes said:
    That's similar to what I wrote in my original posting and what I didn't like because of the need of the array variable (data[] in your example). In terms of "saved memory" it is what I want (except for the fact that I'd define data as const char data[] = { 0x2021, 0x2223, 0}; in order to be able to find the end of the data).

    Oh I see, I didn't quite catch what you were getting at the first time.  Unfortunately, you're probably stuck with creating the extra variable until the bug is fixed.