Declaring Pascal-style strings in C

后端 未结 10 1272
渐次进展
渐次进展 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:40

    It may sound a little extreme but if you have many strings of this kind that need frequent updating you may consider writing your own small tool (a perl script maybe?) that runs on the host system, parses an input file with a custom format that you can design to your own taste and outputs a .c file. You can integrate it to your makefile or whatever and live happily ever after :)

    I'm talking about a program that will convert this input (or another syntax that you prefer):

    s = "foo";
    x = "My string";
    

    To this output, which is a .c file:

    const char s[] = {3, 'f', 'o', 'o'};
    const char x[] = {9, 'M', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g'};
    

提交回复
热议问题