|
-
July 18th, 2002, 10:28 AM
#1
Character Arrays
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
-
July 18th, 2002, 10:48 AM
#2
To declare an array of strings, allocate the space for the largest string:
Code:
char ArrayElement[3][15];
strcpy(ArrayElement[0], "First ELement");
strcpy(ArrayElement[1], "Second Element"); // 14 chars + '\0'
strcpy(ArrayElement[2], "Last Element");
Ce n'est que pour vous dire ce que je vous dis.
-
July 18th, 2002, 10:49 AM
#3
What are you trying to do?
-----
char *pArray = new char[SIZE];
-----
Is that what you want to do?
Kuphryn
-
July 18th, 2002, 10:54 AM
#4
Hi Guys!
Thanks for the answers
Alexey, It was exactly what I needed, Thanks!
-
July 18th, 2002, 01:27 PM
#5
You could also use
Code:
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.
Succinct is verbose for terse
-
July 18th, 2002, 02:29 PM
#6
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
-
July 18th, 2002, 04:00 PM
#7
Well said, that man!
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|