By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Initializer for an array of pointers to arrays of strings

P: n/a

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?
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Steve wrote:
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;
}


--
Martin Ambuhl
Nov 14 '05 #2

P: n/a
Martin Ambuhl wrote:
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

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.