www.digitalmars.com         C & C++   DMDScript  

D - Arrays of strings

reply Ryan Michel <ryan michel.com.au> writes:
I have been scratching my head trying to figure this out.  How to declare a
dynamic array of dynamic strings and 
access then strings.  I have tried the following:

char[]*[] stringArray;
// read from file into char[] tempHolder.  It reads in thew string alright as I
have succesfully displayed iot 
// back to the screen.  This step could probably be done away with and read
staright into the string array.

// then copy into string Array
stringArray[stringArray.length - 1] = new char[tempHolder.length];

//or
char[][] stringArray;
stringArray[stringArray.length - 1] = tempHolder;

Both produce errors.
I have been working on this code for hours.  I would like the strings to be
dynamic by the way so I'm not sure 
whether method 1 is the best way to go.

ryan
Jun 07 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Ryan Michel" <ryan michel.com.au> wrote in message
news:1103_1023449782 news.digitalmars.com...

 I have been scratching my head trying to figure this out.  How to declare
a dynamic array of dynamic strings and char[][] stringlist; stringlist ~= "foo"; stringlist ~= "bar"; stringlist ~= "baz"; for (int i = 0; i < stringlist.length; i++) printf("%.*s\n", stringlist[i]);
Jun 07 2002
prev sibling parent reply "Dario" <supdar yahoo.com> writes:
char[][] stringArray;
makes an empty array of strings. So stringArray.length-1 = -1. Accessing
stringArray[-1] produces a compile-time error.

I also have a problem with strings. What's bad in this?

import c.stdio;
int main()
{
    char*[3] lines =
    [   "Per me si va nella citta' dolente,",
        "Per me si va nell'etterno dolore,",
        "Per me si va tra la perduta gente."
    ];
    blah(lines);
    return 0;
}
void blah(char*[] lines)
{
    for(int i; i < lines.length; ++i) printf('%s' \n, lines[i]);
}

The compiler says:
Assertion failure: 'ie' on line 253 in file 'declaration.c'
abnormal program termination

Is this a compiler bug?
Jun 08 2002
parent reply "Sean L. Palmer" <seanpalmer earthlink.net> writes:
Move the initialized array out of the function.

Sean

"Dario" <supdar yahoo.com> wrote in message
news:adt6mj$lbq$1 digitaldaemon.com...
 char[][] stringArray;
 makes an empty array of strings. So stringArray.length-1 = -1. Accessing
 stringArray[-1] produces a compile-time error.

 I also have a problem with strings. What's bad in this?

 import c.stdio;
 int main()
 {
     char*[3] lines =
     [   "Per me si va nella citta' dolente,",
         "Per me si va nell'etterno dolore,",
         "Per me si va tra la perduta gente."
     ];
     blah(lines);
     return 0;
 }
 void blah(char*[] lines)
 {
     for(int i; i < lines.length; ++i) printf('%s' \n, lines[i]);
 }

 The compiler says:
 Assertion failure: 'ie' on line 253 in file 'declaration.c'
 abnormal program termination

 Is this a compiler bug?
Jun 08 2002
parent reply "Dario" <supdar yahoo.com> writes:
Incredible, that's works. Maybe I'll seem stupid, but why can't I write it
in the function?
Moreover, can I write this?:
    blah( ["one", "two", "three"] );

"Sean L. Palmer" wrote
 Move the initialized array out of the function.

 Sean
Jun 08 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Dario" <supdar yahoo.com> wrote in message
news:adtlvs$14li$1 digitaldaemon.com...
 Incredible, that's works. Maybe I'll seem stupid, but why can't I write it
 in the function?
You can, it just needs to be 'static'. It's a bug in the compiler.
 Moreover, can I write this?:
     blah( ["one", "two", "three"] );
Not yet <g>.
Jun 10 2002
parent reply "Dario" <supdar yahoo.com> writes:
 Incredible, that's works. Maybe I'll seem stupid, but why can't I write
it
 in the function?
You can, it just needs to be 'static'. It's a bug in the compiler.
 Moreover, can I write this?:
     blah( ["one", "two", "three"] );
Not yet <g>.
So it is going to be supported, is it in the specs? Where can I found info about it?
Jun 17 2002
parent "Walter" <walter digitalmars.com> writes:
"Dario" <supdar yahoo.com> wrote in message
news:aekb6u$1huf$1 digitaldaemon.com...
 Incredible, that's works. Maybe I'll seem stupid, but why can't I
write
 it
 in the function?
You can, it just needs to be 'static'. It's a bug in the compiler.
 Moreover, can I write this?:
     blah( ["one", "two", "three"] );
Not yet <g>.
So it is going to be supported, is it in the specs? Where can I found info about it?
It'll get supported, but it isn't in the specs yet. -Walter
Jun 18 2002