473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Initializer for an array of pointers to arrays of strings


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
2 7149
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
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", 0 }; const char* t2 = { "p", "q", 0 }; const char* t3 =...
2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
12
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.