How to generate repetitive characters?

Hi all,

If I have a piece of code like this:

const char *options[] = {
    "voltage\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
    "current\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
    "power\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
};

Instead of manually counting and typing all the "\0" characters, is there a way to do something like this:

const char *options[] = {
    "voltage\0" rep "\0", 16,
    "current\0" rep "\0", 16, 
    "power\0" rep "\0", 16,
};

Or even better:

const char *options[] = {
    "voltage\0" rep "\0", sizeof (double),
    "current\0" rep "\0", sizeof (double), 
    "power\0" rep "\0", sizeof (double),
};

Lastly, notice that there is NOT a comma between "voltage\0" and "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",. The array "options" has only 3 elements, not 6 as it appears at first glance.

Thanks!

-- Roger

is there a way to do something like this:- ......

Sorry no not in C.

You could initialise a blank array and then fill it programmatically if you want.

What happens if you do

const char *options[3][20] = {
    "voltage",
    "current",
    "power"
};

There will be space after the strings but I'm not sure what values will be inserted. Maybe that doesn't matter unless you really need 0s.


Rob

Graynomad:
What happens if you do

const char *options[3][20] = {

"voltage",
    "current",
    "power"
};




There will be space after the strings but I'm not sure what values will be inserted. Maybe that doesn't matter unless you really need 0s. 

______
Rob

Thanks. Interesting idea. I'm not sure that it would work though, because I need to insure that the zeros come immediately after the string in memory. With your method, I think the compiler would put the variables anywhere it wanted to (and not necessarily in a contiguous manner) right?

Not sure, I'm not that clued up on the nuances of compilers, bit of a black art to me :slight_smile: Certainly each 20 bytes will be contiguous.

Why do you need the strings to be zero-filled ?


Rob

This

const char options[3][20] = {
    "voltage",
    "current",
    "power"
};

Aray is 3x20 bytes exactly in memory. It will be zeroed till the end of each string in array but remember there is an identical copy in FLASH and it waste a space.