Initializer for an array of pointers to arrays of strings

S

Steve

I want an initializer for an array of pointers to arrays of strings.

So I can do something like this:

const char *t1[] = { "a", "b", "c", NULL };
const char *t2[] = { "p", "q", NULL };
const char *t3[] = { "w", "x", "y", "z", NULL };
const char **test[] = { t1, t2, t3, NULL };

I was wondering whether the is a more elegant way of writing such an
initializer, one that does away with all the redundant names t1, t2, etc.

For a 2d array of strings I could do:

const char *test2[][5] =
{
{ "a", "b", "c", NULL, NULL },
{ "p", "q", NULL, NULL, NULL },
{ "w", "x", "y", "z", NULL }
};

Is there something similar for arrays of pointers to arrays of strings?
 
M

Martin Ambuhl

Steve said:
I want an initializer for an array of pointers to arrays of strings.

So I can do something like this:

const char *t1[] = { "a", "b", "c", NULL };
const char *t2[] = { "p", "q", NULL };
const char *t3[] = { "w", "x", "y", "z", NULL };
const char **test[] = { t1, t2, t3, NULL };

I was wondering whether the is a more elegant way of writing such an
initializer, one that does away with all the redundant names t1, t2, etc.

For a 2d array of strings I could do:

const char *test2[][5] =
{
{ "a", "b", "c", NULL, NULL },
{ "p", "q", NULL, NULL, NULL },
{ "w", "x", "y", "z", NULL }
};

Is there something similar for arrays of pointers to arrays of strings?

Will this do?

int main(void)
{
const char **test[] = { (const char *[]) {"a", "b", "c", 0},
(const char *[]) {"p", "q", 0},
(const char *[]) {"w", "x", "y", "z", 0},
0 };
return 0;
}
 
S

Steve

Martin said:
Will this do?

int main(void)
{
const char **test[] = { (const char *[]) {"a", "b", "c", 0},
(const char *[]) {"p", "q", 0},
(const char *[]) {"w", "x", "y", "z", 0},
0 };
return 0;

Yes, thanks, that does do the trick.

I notice that my C compiler is ok with it, but my C++ one not. Curious, but
that would be a question for another group.

Thanks again,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top