STRING ARRAYs ... Please Help...

Posted By: NeoJT

STRING ARRAYs ... Please Help... - 04/11/13 17:51

Hi... (and sorry for my english).

I have problem with STRING* ARRAYs.

I want to create and Array of Strings... for example with player names.
And i have tried with...

STRING* g_player_names[50];
or
STRING g_player_names[50];
STRING* g_playername_ptr;

and command such as :

g_player_names[1] = str_create("Test_Player");

str_cpy(g_player_names[1], "Test_Player" );

g_playername_ptr = &g_player_names[1];

*g_playername_ptr = "Test_Player";

but nothing... i receive errors - crashed etc...

//--------------------
only with char* i can create-pass a text... but i want to create a STRING ARRAY.

and a second question ... how can i pass this STRING ARRAY to a FUNCTION ???
I Know that ONLY A Pointer we can pass to a function and not an array... but still nothing...

and i have this problems ( pass to function ) and with the STRUCT ARRAYS... not only for STRING Arrays...

Please Help...


Thx all for your help...

Best Regards
Dimitris.

(and sorry for my english).
Posted By: Uhrwerk

Re: STRING ARRAYs ... Please Help... - 04/11/13 17:59

Use TEXTs instead. A TEXT already contains a string array and a a plus you get several engine functions dealing with TEXT objects.

Concerning the string array you want to create: You're just wildly guessing what to do. This won't work. Have a look in the manual concerning variables and pointers. The correct way to define an array of STRING pointers is "STRING* g_player_names[50];". But don't forget this is just an array with vagabonding pointers in it.
Posted By: sivan

Re: STRING ARRAYs ... Please Help... - 04/11/13 18:14

I use string arrays created runtime, I'll put here an example tomorrow. but search the forum, I remember somebody surely made an example of it not a long time ago...
Posted By: NeoJT

Re: STRING ARRAYs ... Please Help... - 04/11/13 18:49

i want to pass a string array to a function... or a struct array... but i can only pass one string and not all array... for example :

int g_player_id = 0;
STRING* g_player_name[50];

function main()
{
g_player_id++;
g_player_name[g_player_id] = str_create("Test_1_Player");
g_player_id ++;
g_player_name[g_player_id] = str_create("Test_2_Player");

f_print_name(g_player_id-1, g_player_name);
wait_for(f_print_name);
}


function f_print_name(int l_i, STRING* l_name)
{
printf("Player [ %i ] name is : %s", l_i, _chr( l_name ) );
printf("Player [ %i ] name is : %s", (l_i+1), _chr( l_name + 1 ) );
}

//------------------------------------------------------
/*
i use ( l_name + 1 ) because l_name is a pointer and somewhere i read that the l_name[1] and l_name[2]
i can write also as (l_name) and (l_name + 1)...
also i was tried with l_name[1] and l_name[2] but nothing... the result is empty ... (means that the string values cannot pass to function)...

the same problem i have with string thas is within a struct... i cannot pass a struct array to a function...
*/
//------------------------------------------------------

my target is to create a function that can take string arrays or Struct arrays ( with pointers only as i know ) , edit the values and send the result back or change the global value inside that function...

(Function-Command-Parameters like as C# :

stringarray = somestring.split('-');
a function that splits a string and return the result into an array of strings... )

my function must take the array from user such as :

f_split( STRING* l_str_to, STRING* l_str_from, char* l_char_to_split);

or

STRING* l_str_to[100];

l_str_to = f_split(STRING* l_str_from, char* l_char_to_split);

//------------------------------------------
but my problem is that i cannot also pass and struct arrays... :-(

thx you all... (and sorry for my english).

Best Regards.
Dimitris.


Posted By: Uhrwerk

Re: STRING ARRAYs ... Please Help... - 04/11/13 18:59

Again my advice: read the manual and / or a good c book. Don't try this by trial and error. This will come in very very expensive later on. And please use code tags when posting code.

Here is a quick sample that should get you started anyways:
Code:
#include <acknex.h>

STRING* my_string_array[2];


void print_strings(STRING** array,int numberOfElements)
{
	int i;
	for (i = 0; i < numberOfElements; i++)
		printf(array[i]->chars);
}

void main()
{
	my_string_array[0] = str_create("Alpha");
	my_string_array[1] = str_create("Bravo");
	print_strings(my_string_array,2);
}

Posted By: NeoJT

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:08

thank you my friend... i dont know about **

* is pointer and ** is array ???

is that true ??

i tried that to my example and its worked... :-)

I can use ** for array ??? that is something that i just learned...

thx for all...


PS... can you tell me where can i find more details for that ???



THX YOU ALL...

Best Regard.
Dimitris.
Posted By: Uhrwerk

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:16

No, that is not correct.
Code:
STRING* s = NULL; // A pointer to a string struct
STRING** s2 = NULL; // A pointer to a pointer to a string struct
STRING*** s3 = NULL; // A pointer to a pointer to a pointer ...


Keep in mind that an array is just a pointer to a memory area where the content of the array lays one element after another.

I believe JustSid posted some excellent tutorial concerning pointers some time ago: http://eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx
Posted By: NeoJT

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:23

oohhh... i see...

if we want to send a 2d array [][] we must use ***
* first pointer is for the name
* second is for the first []
* third is for the second []

this is it ???
Posted By: WretchedSid

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:27

No. You can have a 2d Array with just one or two "stars". It all boils down to your data type and how you want to interpret it.
If you want a 2d STRING array, you would most likely write it like STRING ***myArray. Please read the link Uhrwerk has posted, it is immensely useful!
Posted By: NeoJT

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:27

and aomething else...

how can i copy two arrays without memcpy(); ??
and not for loop ???

like : array_1 = array_2.

is that ok ?

thx you all.
Posted By: WretchedSid

Re: STRING ARRAYs ... Please Help... - 04/11/13 19:29

Originally Posted By: NeoJT
how can i copy two arrays without memcpy(); ??
and not for loop ???

You can't. Even if you just want a shallow copy. Honestly, please read the Tutorial Uhrwerk linked, it will explain everything to you concerning pointers and memory, and it will make you life much much easier.
© 2024 lite-C Forums