array of string literals

J

Jeff Williams

This doesn't compile:

const char** ids =
{
"aaa",
"bbb",
"ccc"
};

I hope the above illustrates what I am trying to do, if it is possible, what
is the correct syntax?

btw i want to create a static array where each element is a c-style array
(in case my example was unclear)
 
A

Artie Gold

Jeff said:
This doesn't compile:

const char** ids =

const char * ids[] =
{
"aaa",
"bbb",
"ccc"
};

I hope the above illustrates what I am trying to do, if it is possible, what
is the correct syntax?

btw i want to create a static array where each element is a c-style array
(in case my example was unclear)
HTH,
--ag
 
R

Ron Natalie

Jeff Williams said:
This doesn't compile:

const char** ids =

Learn the difference between a pointer and an array. The above is a pointer to
pointer. An array of pointers is:

const char* ids[] =
 
J

Jeff Williams

Artie Gold said:
Jeff said:
This doesn't compile:

const char** ids =

const char * ids[] =
{
"aaa",
"bbb",
"ccc"
};

I hope the above illustrates what I am trying to do, if it is possible, what
is the correct syntax?

btw i want to create a static array where each element is a c-style array
(in case my example was unclear)
HTH,
--ag

Thanks, worked great!

Jeff
 
K

Kevin Goodsell

Jeff said:
This doesn't compile:

const char** ids =
{
"aaa",
"bbb",
"ccc"
};

I hope the above illustrates what I am trying to do, if it is possible, what
is the correct syntax?

btw i want to create a static array where each element is a c-style array
(in case my example was unclear)

Your description of what you want doesn't match the solution you accepted.

const char *ids[] = { "aaa", "bbb", "ccc" };

This does not create an "array where each element is a c-style array."
(By the way, there's no "C++-style arrays", so you don't need to specify
C-style for an array. All arrays are C-style.) It creates an array where
each element is a pointer to a null-terminated character string. To get
what you described, you need something like this:

char ids[][N] = { "aaa", "bbb", "ccc" };

Where N is chosen to be large enough for your longest string (including
the null character).

As for making it static, it might depend on what exactly you mean by
that. Generally you just add the keyword 'static':

static char ids[][N] = { "aaa", "bbb", "ccc" };

But this has different meanings in different contexts, and the meaning
may not be the one you want.

If you meant "statically allocated" and the declaration is at namespace
or global scope, then you shouldn't need the 'static' at all. If the
declaration is in a function, you should add the 'static' keyword. If
the declaration is in a class, you need the 'static' and you also need a
separate definition outside the class, and you should move the
initializer to that definition.

-Kevin
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top