473,289 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Any difference in creating array of strings

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"};
Nov 13 '05 #1
9 4035
sci <sc****@yahoo.com> scribbled the following:
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 (pa*****@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
Nov 13 '05 #2
"sci" <sc****@yahoo.com> wrote in
news:Lp***********************@bgtnsc04-news.ops.worldnet.att.net:
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.

--
- Mark ->
--
Nov 13 '05 #3
On 8 Oct 2003 17:17:07 GMT, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
sci <sc****@yahoo.com> scribbled the following:
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.
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #4
Robert B. Clark <ep****@3pynexf.pbz> scribbled the following:
On 8 Oct 2003 17:17:07 GMT, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
sci <sc****@yahoo.com> scribbled the following:
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 (pa*****@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
Nov 13 '05 #5
On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sc****@yahoo.com> wrote:
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>>
Nov 13 '05 #6
sci
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message
news:bm**********@oravannahka.helsinki.fi...
sci <sc****@yahoo.com> scribbled the following:
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!


Nov 13 '05 #7
sci

Barry Schwarz <sc******@deloz.net> wrote in message
news:bm**********@216.39.134.109...
On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sc****@yahoo.com> wrote:
I believe both ways to create an array of strings are correct. Is there anydifference 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!
Nov 13 '05 #8
sci <sc****@yahoo.com> scribbled the following:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message
news:bm**********@oravannahka.helsinki.fi...
sci <sc****@yahoo.com> scribbled the following:
> 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.
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?
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 (pa*****@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
Nov 13 '05 #9
On Thu, 09 Oct 2003 06:26:27 GMT, "sci" <sc****@yahoo.com> wrote:

Barry Schwarz <sc******@deloz.net> wrote in message
news:bm**********@216.39.134.109...
On Wed, 08 Oct 2003 17:01:31 GMT, "sci" <sc****@yahoo.com> wrote:
>I believe both ways to create an array of strings are correct. Is thereany >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[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].

The second creates a true array of strings.

"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>>
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
1
by: jhcorey | last post by:
I don't know where the actual issue is, but hopefully someone can explain. The following displays "5" in FireFox, but "3" in IE: <script type="text/javascript" language="javascript"> var...
10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
10
by: Chung Leong | last post by:
Is there anything funcationally difference between the following snippets: // first $lines = array(); $line =& $lines; // second $line = array(); $lines =& $line;
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
16
by: gucci09 | last post by:
I was wondering how you would go about creating a dynamically allocated array of strings from a file. i have a dictionary that i want to load in my program in order to spell check. i cant decide if...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
3
by: Author | last post by:
I have always been wondering if there is any significant different between doing System.Console.WriteLine("Employee Name = " + employee.FirstName + " " + employee.LastName); and ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.