[Solved] Displaying an array of strings.

Posted By: Hawthourne

[Solved] Displaying an array of strings. - 12/10/15 02:17

Greetings, I have been exploring arrays a bit in the context of making an inventory. It seems to me that a relatively simple way to do this is by building a string array and then displaying it, most likely with a text box. The gamestudio manual, however, is fairly scarce on its treatment of String arrays, just making some mention of how they are similar to variable arrays.

With this in mind, I tried to directly define an array using STRING* filler[100]. The script is accepts this notation, but when I try to access an entry of it like I would a variable array (arrayname[entrynumber]) I get a launch error. The only message I am given is a ^before the name of the array entry I was trying to pull.

Am I not able to directly define an array like this, rather I have to manually enter in the various string into a text and use that as the array? My current project is displaying a series of variables, so I was going to translate each entry of the variable array over to an entry of the string array. Is there a better path to take?

Bonus: Also, I placed a vec_set(textname.blue,vector(100,100,100)); into my main function to change the text's color, but it remained white. Any ideas what is up with that?

Thanks for any assistance.
Posted By: Anonymous

Re: Displaying an array of strings. - 12/10/15 04:51

God wishes lite-c work in this c++ way

Code:
STRING* filler[100]
TEXT* OBJECT TO DISPLAY STRINGS <--- See manual
usually provide links but the bottles got me...

fucntion this()
{
int arrayi[100];
.... syntex fills array
int i=0;
for(i=0;i<100;i++)
{
str_for_num(filler[i],arrayi[i]
}
}



I am
Mal
Peace love and fun programming
Posted By: Anonymous

Re: Displaying an array of strings. - 12/10/15 04:52

Rest of your post is lost to my drunkenness... Thnk night
Posted By: Florastamine

Re: Displaying an array of strings. - 12/10/15 06:31

You have to replace STRING *filler[] with a pointer to pointer:
Code:
#include <acknex.h>

#define N 10

int main(void)
{
	STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);
	
	int i = 0;
	for(; i < N; i++)
	    array[i] = str_create(str_printf(NULL, "This is string #%i", i));
	
	for(i = 0; i < N; i++)
	    printf("%s", _chr(array[i]));
	    
	return 0;
}



Another way would be using TEXT.

Quote:
Also, I placed a vec_set(textname.blue,vector(100,100,100)); into my main function to change the text's color, but it remained white. Any ideas what is up with that?


You have to "tell" Acknex that the text object can be colored, by passing an additional "LIGHT" flag:

Code:
#include <acknex.h>

TEXT *text =
{
	strings = 1;
	string("Hello!");
	
	font = "Arial#25b";
	
	flags = SHOW | LIGHT;
}

int main(void)
{
	random_seed(0);
	
	while( !key_esc )
	{
		vec_set(text.blue,vector(random(255.0), random(255.0), random(255.0)));
		wait( -0.5 );
	}
	return 0;
}

Posted By: EpsiloN

Re: Displaying an array of strings. - 12/10/15 08:02

You will display this text, so why not use a text directly...
Define a text and follow the manual examples:
Code:
TEXT* inventory_text =	
{
    layer = 1;
    pos_x = 10;
    pos_y = 10;
    strings = 100;
    flags = LIGHT | SHOW;
}



Now, to access individual strings from this text use:
Code:
(inventory_text.pstring)[0]


And I suggest never try to modify this directly. Always use str_cpy or you can get in a lot of trouble without understanding where your error is grin
Code:
str_cpy((inventory_text.pstring)[0],"Short sword");
...
str_cpy((inventory_text.pstring)[1],axeName_str);



Originally Posted By: "Florastamine"
You have to replace STRING *filler[] with a pointer to pointer:

Your answer is great, but...
If he's new to this, I doubt he'll understand what a pointer to pointer is laugh I'm not new, but I just scratched the surface of C++, so even I get confused sometimes...
Posted By: Hawthourne

Re: Displaying an array of strings. - 12/10/15 22:58

Thanks, I got it now. I guess there is a difference between array.pstring[1] and (array.pstring)[1], who would have known? XD

I also loled at Malice's reply,
Posted By: Anonymous

Re: Displaying an array of strings. - 12/11/15 05:52

Quote:
I also loled at Malice's reply,


I am quite ridiculous, for sure!
Quote:
If he's new to this, I doubt he'll understand what a pointer to pointer is laugh I'm not new, but I just scratched the surface of C++, so even I get confused sometimes...


In this usage, it's bit complex for more reasons then pointer to a pointer.
Quote:
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);


However the idea of pointer to pointer is not difficult. As In player=my; when written clean in lite-c, is simply a pointer-to-a-pointer, not a copy of the object. An address to a address, but more a copy of a memory address.

But perhaps, my understanding is just poor, in this matter and C++.

Mal
Posted By: txesmi

Re: Displaying an array of strings. - 12/11/15 16:26

Originally Posted By: Florastamine

Code:
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);



This code line is wrong. It allocates N times the length of a STRING struct!

It should be:
Code:
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);



that allocates N times the length of a pointer to a STRING struct.

Salud!
Posted By: Florastamine

Re: Displaying an array of strings. - 12/12/15 04:18

Originally Posted By: txesmi
Originally Posted By: Florastamine

Code:
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);



This code line is wrong. It allocates N times the length of a STRING struct!

It should be:
Code:
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);



that allocates N times the length of a pointer to a STRING struct.

Salud!


Yeah, I forgot about the extra asterisk! Because it's a pointer to pointer, you should allocate memory for the pointers.

Thanks for pointing it out!
Posted By: tagimbul

Re: Displaying an array of strings. - 04/01/16 07:16

is this correct?
Code:
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);
...

array[1] = "hello";



can i use array with [] ?
Posted By: txesmi

Re: Displaying an array of strings. - 04/01/16 15:14

Yes, you can use array with [] but you need to create strings with str_create.

Code:
array[0] = str_create ( "First string" );
array[1] = str_create ( "Second string" );
...
str_cpy ( array[1], "..." );
// or
str_cpy ( *(array + 1), "..." );
...
str_remove ( array[0] );
str_remove ( array[1] );
...
sys_free ( array );



Salud!
Posted By: EpsiloN

Re: Displaying an array of strings. - 04/01/16 15:41

I suggest you read "The C Programming Language" by K&R and you'll understand pointers and other complex stuff.

You can use a pointer like "temp[5]" or "temp + 5" which is the same thing when you're dealing with a pointer, that's why you need to read the book laugh its amazing.
Posted By: WretchedSid

Re: Displaying an array of strings. - 04/01/16 21:04

5[temp] also works. In any case though, a more modern book on C might be better to get people into the language, the R of K&R died in 2011 and the book is from 1988!
Posted By: tagimbul

Re: Displaying an array of strings. - 04/02/16 14:36

i have it thx^^

Code:
#include <acknex.h>
#include <default.c>

var* alloziiere_2()
{
	var j = 10;
	return (sys_malloc(sizeof(var) * j));	
}
var *Dialogue_Nummer;

function main ()
{
	
	Dialogue_Nummer = alloziiere_2 ();
 
	Dialogue_Nummer[1] = 123;
	while(1)
	{
	DEBUG_VAR(Dialogue_Nummer[1] , 34);
	wait(1);
	}
}

Posted By: tagimbul

Re: Displaying an array of strings. - 04/02/16 14:42

oh and with string:


Code:
#include <acknex.h>
#include <default.c>

STRING **str_Dialogue_Array;
STRING* alloziiere_1()
{
	var i = 10;
	return malloc(sys_malloc (sizeof(STRING*) * i));	
}

function main()
{
str_Dialogue_Array = alloziiere_1();

str_Dialogue_Array[i] = str_create("");

}



its a global initalized String array with dynamic size ^^ <3
Posted By: WretchedSid

Re: Displaying an array of strings. - 04/02/16 16:17

That code does not even remotely do what you think it does. For starters, array indices start at 0 and not at 1. But even worse, malloc(sys_malloc(...)) is absolutely nonsensical, use either one or the other! Both allocate memory and return a pointer to it, so by feeding the result of sys_malloc() to malloc() means you tell malloc() to allocate a memory chunk the size of whatever value the pointer has, so you might be easily allocating a gigabyte or more here. And on top of that, you leak the memory returned by sys_malloc() (obviously only a tiny a fraction of what the malloc() could potentially allocate)
Posted By: tagimbul

Re: Displaying an array of strings. - 04/02/16 16:29

oh sorry i mean
Code:
return (sys_malloc (sizeof(var*) * i));	
//and
return (sys_malloc (sizeof(STRING*) * i));



that was a typo
© 2024 lite-C Forums