Any difference in creating array of strings

S

sci

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"};
 
J

Joona I Palaste

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
 
M

Mark A. Odell

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, the first will use only the memory need to store the strings which is
nice if you have many different length strings or you don't want to worry
about what happens if you make some strings really long.

The second will always occupy 10 x 30 chars and you will get a compile
error if any of the strings exceeds 9 + 1 chars.
 
R

Robert B. Clark

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
2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
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

299? 10 x 30 = 300.
 
J

Joona I Palaste

Robert B. Clark said:
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
2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
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
299? 10 x 30 = 300.

Thanks for the correction. I need more experience in mathematics...

--
/-- 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! ------------/
"It's not survival of the fattest, it's survival of the fittest."
- Ludvig von Drake
 
B

Barry Schwarz

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"};

The first does not create an array of strings. It creates an array of
pointers. Each pointer points to a string but that is not the same
thing.

The second creates a true array of strings.


<<Remove the del for email>>
 
S

sci

Joona I Palaste said:
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"};

ii) The strings in (2) are guaranteed to be modifiable. The strings in
(1) are not.

Why strings in (1) are not modifiable?
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.

Why you can return strings from (1)? Are you talking about returning the
whole "array of strings" in (1) or just one of the strings?

Thanks for your help!
 
S

sci

Barry Schwarz 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"};

The first does not create an array of strings. It creates an array of
pointers. Each pointer points to a string but that is not the same
thing.

Why they're not the same things? I thought they both give a means for an
array of strings.
The second creates a true array of strings.
"True array"? I really want to learn more about this.

Thanks for your help!
 
J

Joona I Palaste

sci said:
Joona I Palaste said:
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"};
ii) The strings in (2) are guaranteed to be modifiable. The strings in
(1) are not.
Why strings in (1) are not modifiable?

They *can* be. They just aren't *guaranteed* to be. The C standard
allows the implementation to place them in unmodifiable memory.
Why you can return strings from (1)? Are you talking about returning the
whole "array of strings" in (1) or just one of the strings?

Just one of the strings. For example, return MyString[0]; is a
sensible thing to do in case (1) but not in case (2). But return
MyString; is not sensible in either case.
Thanks for your help!

You're welcome.

--
/-- 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! ------------/
"You could take his life and..."
- Mirja Tolsa
 
B

Barry Schwarz

Barry Schwarz 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"};

The first does not create an array of strings. It creates an array of
pointers. Each pointer points to a string but that is not the same
thing.

Why they're not the same things? I thought they both give a means for an
array of strings.

Consideration one:

In case 1, sizeof MyString will evaluate to a value equal to
30*sizeof(char*) which on many systems will be 120. I cannot think of
any system where it would be larger than 240. In any event, it is not
possible for 10 strings, each of size 30, to be contained in anything
less that 300 bytes.

In case 2, sizeof MyString will always evaluate to 300.

Consideration two:

In an array of strings, the first character of the each string
must be the same distance from the first character of each adjacent
string.

This is true in case 2. &Mystring[i+1][0]-&MyString[0] will
evaluate to 30 for all i for which the expression is defined (all i
between 0 and 8 inclusive). In fact, the expression is a compile time
constant which can be evaluated by the compiler and need not generate
any code.

In case 1, it is not even legal to attempt to compute
&MyString[1][0]-&MyString[0][0].
"True array"? I really want to learn more about this.

s/true/actual/ or s/true/real/ or s/true/valid/.



<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top