Connecting Tech Pros Worldwide Forums | Help | Site Map

Any difference in creating array of strings

sci
Guest
 
Posts: n/a
#1: Nov 13 '05
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"};



Joona I Palaste
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Any difference in creating array of strings


sci <sci998@yahoo.com> scribbled the following:[color=blue]
> I believe both ways to create an array of strings are correct. Is there any
> difference between these two?[/color]
[color=blue]
> 1. char *MyString[30] = {"First string", "Second string", ..."Tenth
> string"};[/color]
[color=blue]
> 2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
> string"};[/color]

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 (palaste@cc.helsinki.fi) ---------------------------\
| 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
Mark A. Odell
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Any difference in creating array of strings


"sci" <sci998@yahoo.com> wrote in
news:LpXgb.169000$0v4.12833661@bgtnsc04-news.ops.worldnet.att.net:
[color=blue]
> 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"};[/color]

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.

--
- Mark ->
--
Robert B. Clark
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Any difference in creating array of strings


On 8 Oct 2003 17:17:07 GMT, Joona I Palaste <palaste@cc.helsinki.fi> wrote:
[color=blue]
>sci <sci998@yahoo.com> scribbled the following:[color=green]
>> I believe both ways to create an array of strings are correct. Is there any
>> difference between these two?[/color]
>[color=green]
>> 1. char *MyString[30] = {"First string", "Second string", ..."Tenth
>> 2. char MyString[10][30] = {"First string", "Second string", ..."Tenth[/color][/color]
[color=blue]
>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[/color]

299? 10 x 30 = 300.


--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Joona I Palaste
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Any difference in creating array of strings


Robert B. Clark <epynex@3pynexf.pbz> scribbled the following:[color=blue]
> On 8 Oct 2003 17:17:07 GMT, Joona I Palaste <palaste@cc.helsinki.fi> wrote:[color=green]
>>sci <sci998@yahoo.com> scribbled the following:[color=darkred]
>>> I believe both ways to create an array of strings are correct. Is there any
>>> difference between these two?[/color]
>>[color=darkred]
>>> 1. char *MyString[30] = {"First string", "Second string", ..."Tenth
>>> 2. char MyString[10][30] = {"First string", "Second string", ..."Tenth[/color][/color][/color]
[color=blue][color=green]
>>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[/color][/color]
[color=blue]
> 299? 10 x 30 = 300.[/color]

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

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| 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
Barry Schwarz
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Any difference in creating array of strings


On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sci998@yahoo.com> wrote:
[color=blue]
>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"};
>[/color]

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>>
sci
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Any difference in creating array of strings


Joona I Palaste <palaste@cc.helsinki.fi> wrote in message
news:bm1gqj$ep3$1@oravannahka.helsinki.fi...[color=blue]
> sci <sci998@yahoo.com> scribbled the following:[color=green]
> > I believe both ways to create an array of strings are correct. Is there[/color][/color]
any[color=blue][color=green]
> > difference between these two?[/color]
>[color=green]
> > 1. char *MyString[30] = {"First string", "Second string", ..."Tenth
> > string"};[/color]
>[color=green]
> > 2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
> > string"};[/color]
>[/color]
[color=blue]
> ii) The strings in (2) are guaranteed to be modifiable. The strings in
> (1) are not.[/color]

Why strings in (1) are not modifiable?
[color=blue]
> 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.[/color]

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!




sci
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Any difference in creating array of strings



Barry Schwarz <schwarzb@deloz.net> wrote in message
news:bm2ksm$c57$1@216.39.134.109...[color=blue]
> On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sci998@yahoo.com> wrote:
>[color=green]
> >I believe both ways to create an array of strings are correct. Is there[/color][/color]
any[color=blue][color=green]
> >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"};
> >[/color]
>
> 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.[/color]

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

Thanks for your help!


Joona I Palaste
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Any difference in creating array of strings


sci <sci998@yahoo.com> scribbled the following:[color=blue]
> Joona I Palaste <palaste@cc.helsinki.fi> wrote in message
> news:bm1gqj$ep3$1@oravannahka.helsinki.fi...[color=green]
>> sci <sci998@yahoo.com> scribbled the following:[color=darkred]
>> > I believe both ways to create an array of strings are correct. Is there[/color][/color]
> any[color=green][color=darkred]
>> > difference between these two?[/color]
>>[color=darkred]
>> > 1. char *MyString[30] = {"First string", "Second string", ..."Tenth
>> > string"};[/color]
>>[color=darkred]
>> > 2. char MyString[10][30] = {"First string", "Second string", ..."Tenth
>> > string"};[/color][/color][/color]
[color=blue][color=green]
>> ii) The strings in (2) are guaranteed to be modifiable. The strings in
>> (1) are not.[/color][/color]
[color=blue]
> Why strings in (1) are not modifiable?[/color]

They *can* be. They just aren't *guaranteed* to be. The C standard
allows the implementation to place them in unmodifiable memory.
[color=blue][color=green]
>> 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.[/color][/color]
[color=blue]
> 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?[/color]

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.
[color=blue]
> Thanks for your help![/color]

You're welcome.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| 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
Barry Schwarz
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Any difference in creating array of strings


On Thu, 09 Oct 2003 06:26:27 GMT, "sci" <sci998@yahoo.com> wrote:
[color=blue]
>
>Barry Schwarz <schwarzb@deloz.net> wrote in message
>news:bm2ksm$c57$1@216.39.134.109...[color=green]
>> On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sci998@yahoo.com> wrote:
>>[color=darkred]
>> >I believe both ways to create an array of strings are correct. Is there[/color][/color]
>any[color=green][color=darkred]
>> >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"};
>> >[/color]
>>
>> 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.[/color]
>
>Why they're not the same things? I thought they both give a means for an
>array of strings.[/color]

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[i][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].
[color=blue]
>[color=green]
>>
>> The second creates a true array of strings.
>>[/color]
>"True array"? I really want to learn more about this.[/color]

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



<<Remove the del for email>>
Closed Thread