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 ANSI C compiler).
A C-string is 0
terminated, eg. {'f'
,'o'
,'o'
,0
}.
A Pascal-string has the length in the first byte, eg. {3
,'f'
,'o'
,'o'
}.
I can declare a C-string to be placed in ROM with:
const char *s = "foo";
For a Pascal-string, I could manually specify the length:
const char s[] = {3, 'f', 'o', 'o'};
But, this is awkward. Is there a better way? Perhaps in the preprocessor?
The string in Pascal is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. Extended Pascal provides numerous types of string objects depending upon the system and implementation.
A character is a single keyboard symbol. It is not a sequence of symbols like a string, but an individual “lonely” symbol. Characters can be placed in various categories: the ten decimal digits: 0, 1, 2, 3,..., 9 the 26 lower-case letters: a, b, c,..., z the 26 upper-case letters: A, B, C,..., Z.
I think the following is a good solution, but don't forget to enable packed structs:
#include <stdio.h> #define DEFINE_PSTRING(var,str) const struct {unsigned char len; char content[sizeof(str)];} (var) = {sizeof(str)-1, (str)} DEFINE_PSTRING(x, "foo"); /* Expands to following: const struct {unsigned char len; char content[sizeof("foo")];} x = {sizeof("foo")-1, "foo"}; */ int main(void) { printf("%d %s\n", x.len, x.content); return 0; }
One catch is, it adds an extra NUL byte after your string, but it can be desirable because then you can use it as a normal c string too. You also need to cast it to whatever type your external library is expecting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With