Connecting Tech Pros Worldwide Help | Site Map

array of strings?

Robert
Guest
 
Posts: n/a
#1: Nov 13 '05
Hi,

Is there some way of using an array of strings? Like in basic?
I know you have to create an array of chars so i think it has to
be an 2d array or something...

Really stuck here... Thanks in advance,

Robert
Irrwahn Grausewitz
Guest
 
Posts: n/a
#2: Nov 13 '05

re: array of strings?


Robert <R.Mens@hetnet.nl> wrote:
[color=blue]
>Hi,
>
>Is there some way of using an array of strings? Like in basic?
>I know you have to create an array of chars so i think it has to
>be an 2d array or something...[/color]

Your problem is currently addressed in todays thread starting off
with message 3f606d28$1@clarion.carno.net.au,
Subject: Please help! char* problems!
[color=blue]
>
>Really stuck here... Thanks in advance,
>
>Robert[/color]

Regards

Irrwahn
--
Close your eyes and press escape three times.
Jirka Klaue
Guest
 
Posts: n/a
#3: Nov 13 '05

re: array of strings?


Robert wrote:[color=blue]
> Is there some way of using an array of strings? Like in basic?
> I know you have to create an array of chars so i think it has to
> be an 2d array or something...[/color]

char *arofstr[] = {"foo", "bar", "baz"};

Jirka

Robert
Guest
 
Posts: n/a
#4: Nov 13 '05

re: array of strings?


Robert wrote:
[color=blue]
> Hi,
>
> Is there some way of using an array of strings? Like in basic?
> I know you have to create an array of chars so i think it has to
> be an 2d array or something...
>
> Really stuck here... Thanks in advance,
>
> Robert[/color]

Alright, because i didn't was very clear (srry for that) i'll
try again... I am used to basic so here is an example of what i
mean in basic...

dim somestring(1 to 10) as string

I maybe need to resize both the array and the actual string
in runtime so can someone please give me an example of that?

btw i looked in that other topic but didn't find where i was
searching for...
Lew Pitcher
Guest
 
Posts: n/a
#5: Nov 13 '05

re: array of strings?


On Thu, 11 Sep 2003 17:41:38 +0200, Robert <R.Mens@hetnet.nl> wrote:
[color=blue]
>Hi,
>
>Is there some way of using an array of strings? Like in basic?
>I know you have to create an array of chars so i think it has to
>be an 2d array or something...[/color]

There are at least a couple of ways to define/use an "array of strings".

First off, strings (in C) are arrays of char, with contents that are terminated
by a \0 char.

Thus
char String[10] = "string";
is a string, as is
char *String = "string";
(actually, in this case, the String variable is a pointer, but it points to an
array of char, terminated with a \0, so it points to a "string");

So, how do we get an array of strings? We get arrays of arrays of chars, or
arrays of pointers to chars.

Thus
char StringArray[3][10] = {"string1", "string2", "string3"};
is an array of strings, as is
char *StringArray[3] = {"string1", "string2", "string3"};

Does that help?
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Morris Dovey
Guest
 
Posts: n/a
#6: Nov 13 '05

re: array of strings?


Robert wrote:
[color=blue]
> I maybe need to resize both the array and the actual string
> in runtime so can someone please give me an example of that?[/color]

Robert...

You might find the functions at:

http://www.iedu.com/mrd/c/tokenize.c and
http://www.iedu.com/mrd/c/tokfile.c

of some interest. These programs defer allocation of storage
until the exact size of the storage needed is known (for both
pointer and char arrays.)

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Closed Thread