sci said:
I believe both ways to create an array of strings are correct. Is there any
difference between these two?
1. char *MyString[30] = {"First string", "Second string", ..."Tenth
string"};
2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
string"};
Yes, and plenty of it.
i) The raw characters of (2) reside consecutively from the address
(char *)MyString to the address (char *)MyString + 2999. The raw
characters of (1) can reside anywhere in memory, not necessarily
consecutively, or even in the right order.
ii) The strings in (2) are guaranteed to be modifiable. The strings in
(1) are not.
iii) You can safely return the strings from (1) as function return
values, they're always in scope. You can't do that for the strings
from (2) as they are only in scope when MyString is.
iv) You can assign new strings to the array in (1) with the = operator.
For the array in (2) you have to use strcpy.
There are other, more subtle differences, but that's basically the lot.
--
/-- Joona Palaste (
[email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
|
http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo