How can I work with a STRING* [ ]?

Posted By: 3Dski

How can I work with a STRING* [ ]? - 03/17/07 20:13

I can declare a STRING* array, but cannot seem to use the LiteC string functions on the elements. I did discover that I can simply initialize an element using "=". But, if I try to use str_cat() to append to any of these:

Code:
 
STRING* mystrings[2];

mystrings[0] = "some string";
mystrings[1] = "another";
...
str_cat( mystrings[0]," one");


I get "Invalid arguments in LiteC." What are the rules for working with arrays of type STRING*?
Posted By: TWO

Re: How can I work with a STRING* [ ]? - 03/17/07 20:54

STRING* mystrings[2];

str_cpy(mystrings[0], "some string");

This should work;
Posted By: 3Dski

Re: How can I work with a STRING* [ ]? - 03/17/07 21:10

One think your str_cpy() would work with the array element, but it doesn't. And, here's an interesting twist... if the second argument to one of LiteC's string functions is an element of a string[], it will accept this:

Code:

STRING* plain = "a string";
STRING* strarray [2];
strarray[0] = " appended 1";
strarray[1] = " appended 2";

str_cat( plain, strarray[0]); // ...good
str_cat( plain, strarray[1]); // ...good
str_cat( plain, " bla,bla"); // ...good

str_cat( strarray[0], plain); // ... invalid arguments error
str_cat( strarray[0], " something"); // ... invalid arguments error



It's almost like the compiler sees a STRING* [] element as a char*!
Posted By: Excessus

Re: How can I work with a STRING* [ ]? - 03/18/07 12:08

I think you must str_create them. STRING is a struct with a number of members. For example a char[] for the contents, a length variable and a pointer to the next element in the internal linked list. str_create is used to initialise all these correctly.

STRING* mystr[3];
mystr[0] = str_create();

Then use it like you would in c-script.
Posted By: 3Dski

Re: How can I work with a STRING* [ ]? - 03/19/07 00:59

Thanks, I'll try that technique. Tell you the truth, I didn't notice a str_create(). I'll research that, try it out and report back it I'm having unexpected problems.

EDIT: Works great, now that I know about the str_create()... that was the ticket. I'm now surprised that it wouldn't complain about setting a string* with = and a string constant
© 2024 lite-C Forums