Click to See Complete Forum and Search --> : Character Arrays


JennyM
July 18th, 2002, 10:28 AM
Hi,
I need to have an array so as to look like the following:

ArrayElement[1] = "First ELement";
ArrayElement[2] = "Second Element";

etc...

How do I declare this?

ie Is it
char* ArrayElement[3];

Or how is it declared?

Thanks in advance,
Jenny

Alexey B
July 18th, 2002, 10:48 AM
To declare an array of strings, allocate the space for the largest string:char ArrayElement[3][15];

strcpy(ArrayElement[0], "First ELement");
strcpy(ArrayElement[1], "Second Element"); // 14 chars + '\0'
strcpy(ArrayElement[2], "Last Element");

kuphryn
July 18th, 2002, 10:49 AM
What are you trying to do?

-----
char *pArray = new char[SIZE];
-----

Is that what you want to do?

Kuphryn

JennyM
July 18th, 2002, 10:54 AM
Hi Guys!

Thanks for the answers

Alexey, It was exactly what I needed, Thanks!

;)

cup
July 18th, 2002, 01:27 PM
You could also use

const char* pArray[] =
{
"First Element",
"Second Element",
"Third Element"
};

Then there is no need to figure out how many you have or the size of the largest string.

jfaust
July 18th, 2002, 02:29 PM
Just because it should be said:

An array of std::string would be better.

A std::vector of std::string would be even better.

Jeff

Graham
July 18th, 2002, 04:00 PM
Well said, that man! :)