0

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.

1
  • In which language is this? Commented May 22, 2014 at 23:07

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.