Declaring Pascal-style strings in C

后端 未结 10 1234
渐次进展
渐次进展 2020-12-25 13:18

In C, is there a good way to define length first, Pascal-style strings as constants, so they can be placed in ROM? (I\'m working with a small embedded system with a non-GCC

10条回答
  •  孤城傲影
    2020-12-25 13:51

    You can define an array in the way you like, but note that this syntax is not adequate:

    const char *s = {3, 'f', 'o', 'o'};
    

    You need an array instead of a pointer:

    const char s[] = {3, 'f', 'o', 'o'};
    

    Note that a char will only store numbers up to 255 (considering it's not signed) and this will be your maximum string length.

    Don't expect this to work where other strings would, however. A C string is expected to terminate with a null character not only by the compiler, but by everything else.

提交回复
热议问题