Reputation: 11
What I want to do is this:
#define A 1
#define B 2
#define C 99
const char row1[] = {A|B, B, A, C};
const char row2[] = {B, A, C};
...
const char row99[] = {B, A, B ,A, A, C};
const char *test[]= {row1, row2, ... , row99};
My question is how can I achieve the above with something like this:
#define A 1
#define B 2
#define C 99
const char *test[] = {
{A|B, B, A, C},
{B, A, C},
....
{B, A, B ,A, A, C}
}
I do not want fixed length 2D array like this:
test[][5] = { {A|B, B, A, C}, {B, A, C}, ...
and also I need the #define and use those token in the initialization.
much appreciated if anybody can show me the correct syntax to do this. thnx.
Upvotes: 0
Views: 56
Reputation: 141544
I'm guessing this refers to C. In C99 you can do this:
char const *const test[] = {
(char const[]){1, 2, 3, 4},
(char const[]){1, 2, 3},
(char const[]){1, 2, 3, 4, 5, 6, 7}
};
The (typename[]){initializer-list}
syntax is called a compound literal. Before C99 the only type of literal is a string literal, and what you want to do isn't possible; you'd have to use your first method, or make significant changes to your system of defines.
Note that in this code there is no way of telling how long each series of char is. You will need to add some method of determining the length (e.g. a sentinel value on the end).
Upvotes: 1