Array of strings

Posted By: roBurky

Array of strings - 01/17/09 00:43

I am trying to create an array of strings, but I'm being given a syntax error. Is what I'm trying to do possible in lite-c? My programming experience is in Java.
Code:
	STRING imageGreen[2] = { "screen_nointruders.bmp", "screen_knowyourenemy.bmp" };


Posted By: Blade280891

Re: Array of strings - 01/17/09 00:50

Erm im not sure how to do it direct but try
STRING * imageGreen[2] = { "screen_nointruders.bmp", "screen_knowyourenemy.bmp" };
or
STRING * imageGreen[2];
imageGreen[1] = "screen_nointruders.bmp";//Should this not be 0, or does lite-c start at 1
imageGreen = "screen_knowyourenemy.bmp";
Posted By: roBurky

Re: Array of strings - 01/17/09 01:14

Thanks for responding, Blade.

If I try your second suggestion, then the first line goes through ok, but the second and third lines get another error message.
Quote:
Can not convert 'ARRAY' to 'struct STRING'

Posted By: badapple

Re: Array of strings - 01/17/09 03:32

dont know if this helps you,its taken right from the manual and looks like a way of creating an array of strings to me



TEXT willkommen
{
...
strings = 5; // 5 differenet string pointes
string = willkommen, dies_ist, ein, text,string1;
}

...

my_text.string[3] = new_string;
Posted By: Nowherebrain

Re: Array of strings - 01/17/09 05:51

I thought you would use....

willkomen.pstring[0] = "_words and numbers and such 1!!";
willkomen.pstring[1];
willkomen.pstring[2];
willkomen.pstring[3];
willkomen.pstring[4];

that would be 5 strings.
and my_text.pstring would be wrong...you would use the name of the TEXT* object/element
Posted By: roBurky

Re: Array of strings - 01/17/09 14:43

Thank you, I've made progress due to your replies, badapple and Nowherebrain. Looking at the command help for pstring gave an example of an array of strings, which I've used to create the array successfully (I think).

Code:
TEXT tScreenGreen =
{
string ("screen_knowyourenemy.bmp", "screen_nointruders.bmp");
}



And then through a lot more trial and error, figured out that I needed to use this to access it:
Code:
(tScreenGreen.pstring)[0]


So thanks everyone! I'm back on track.
Posted By: TechMuc

Re: Array of strings - 01/17/09 17:37

the correct syntax for a string array

STRING** tst;
tst = malloc(sizeof(STRING*),number of elements);
for(int i=0;i<number of elements;i++)
tst[i]=str_create("#number of characters");

now you can access the string array like this:

str_cpy(tst[0],"hallo");
Posted By: MMike

Re: Array of strings - 01/23/09 22:46

Nice trick..
Posted By: Ottawa

Re: Array of strings - 01/23/09 23:52

Hi!

Thanks for the information TechMuc. That might be the solution to my post.

I think that there is a syntax error in the first line that was presented
by roBurky
STRING imageGreen[2] = { "screen_nointruders.bmp", "screen_knowyourenemy.bmp" };
should have been
STRING imageGreen[2] = ( "screen_nointruders.bmp", "screen_knowyourenemy.bmp" );

Ottawa smile
Posted By: Vorick

Re: Array of strings - 02/26/09 10:58

Sorry to dig up a post that's been inactive for a month. I hope this doesn't count as resurrecting dead threads.

I'm currently working with string arrays myself in order to generate an onscreen-log for the server-side of my game. Although techmuc's post provided me with the solution to my problem, there seems to be a typo in it and I just want to spare anyone else finding this topic the headache.

malloc uses only one parameter and therefore you have to multiply the number of elements with sizeof(STRING*). The code below should be the correct one and is working in my scenario.

Code:
STRING** tst;
tst = malloc(sizeof(STRING*) * number of elements);
for(int i=0;i<number of elements;i++)
tst[i]=str_create("#number of characters");

//now you can access the string array like this:

str_cpy(tst[0],"hallo");

Posted By: EvilSOB

Re: Array of strings - 02/27/09 00:27

Yorick and others beware, I dont know it its been fixed yet or not,
but the "sizeof" command is/was only a macro so could fail in this situation.
Often if will fail when combined with other mathematic functions.

The fix is like so
[b]tst = malloc((int)sizeof(STRING*) * number of elements);[b]
or
[b]tst = malloc((long)sizeof(STRING*) * number of elements);[b]
© 2024 lite-C Forums