|
Re: Array of strings and size
[Re: sdelatorre]
#472353
04/22/18 21:58
04/22/18 21:58
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hi! This might not be the best solution, but this is how I handle such stuff:
var total_str = 0;
STRING *list_str[999];
void add_to_str(STRING *str){
list_str[total_str] = str_create(str);
total_str++;
}
void test_list(scancode){
if(scancode == 2){ add_to_str("EUR"); }
if(scancode == 3){ add_to_str("USD"); }
if(scancode == 4){ add_to_str("AUD"); }
}
void main(){
on_1 = test_list; // add EUR to list
on_2 = test_list; // add USD to list
on_3 = test_list; // add AUD to list
int i = 0;
while(!key_esc){
// show total amount of strings
DEBUG_VAR(total_str, 10);
// cycle through all list and show it's elements
for(i = 0; i < total_str; i++){
draw_text(list_str[i], 10, 30 + 20 * i, COLOR_WHITE);
}
wait(1);
}
}
Best regards! Edit: or version with a structure, so you could create lists:
typedef struct {
var array_size;
STRING *str[999];
} LIST;
LIST *currency;
void add_to_str(LIST *pointer, STRING *str){
pointer->str[pointer->array_size] = str_create(str);
pointer->array_size++;
}
void test_currency(scancode){
if(!currency){ currency = sys_nxalloc(sizeof(LIST)); }
if(scancode == 2){ add_to_str(currency, "EUR"); }
if(scancode == 3){ add_to_str(currency, "USD"); }
if(scancode == 4){ add_to_str(currency, "AUD"); }
}
void main(){
on_1 = test_currency; // add EUR to list
on_2 = test_currency; // add USD to list
on_3 = test_currency; // add AUD to list
int i = 0;
while(!key_esc){
// if currency exist ?
if(currency){
// show total amount of strings
DEBUG_VAR(currency->array_size, 10);
// cycle through all list and show it's elements
for(i = 0; i < currency->array_size; i++){
draw_text(currency->str[i], 10, 30 + 20 * i, COLOR_WHITE);
}
}
wait(1);
}
}
It would be cool to add initialisation inside of the 'add_to_str' function, but I couldn't get it working..
|
|
|
Re: Array of strings and size
[Re: sdelatorre]
#472365
04/23/18 09:47
04/23/18 09:47
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Thanks, but
I want only an array of strings and the size of this string....all that for this?
This is list of arrays and it's size is 'total_str'.
var total_str = 0;
STRING *list_str[999];
void add_to_str(STRING *str){
list_str[total_str] = str_create(str);
total_str++;
}
void test_list(scancode){
if(scancode == 2){ add_to_str("EUR"); }
if(scancode == 3){ add_to_str("USD"); }
if(scancode == 4){ add_to_str("AUD"); }
}
void main(){
on_1 = test_list; // add EUR to list
on_2 = test_list; // add USD to list
on_3 = test_list; // add AUD to list
int i = 0;
while(!key_esc){
// show total amount of strings
DEBUG_VAR(total_str, 10);
// cycle through all list and show it's elements
for(i = 0; i < total_str; i++){
draw_text(list_str[i], 10, 30 + 20 * i, COLOR_WHITE);
}
wait(1);
}
}
|
|
|
Re: Array of strings and size
[Re: 3run]
#472366
04/23/18 10:36
04/23/18 10:36
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Do you like risk?
#define SIZE_OF_CURRENCIES (((long)&chrCurLast - (long)&chrCur00) / sizeof(char*))
char *chrCur00 = "EUR"; //
char *chrCur01 = "USD";
char *chrCur02 = "AUD";
char *chrCurLast = "";
int _i = 0;
for (; _i<SIZE_OF_CURRENCIES; _i+=1) {
char *_chrCur = *(&chrCur00 + _i);
edited_______________ or with no index at all
char **_chrCur = &chrCur00;
for (; _chrCur<&chrCurLast; _chrCur+=1) {
...
Just noticed global variables are sorted alphabetically. edited 2 _______________________ Just realized that you might build the array into a struct with no risk at all.
typedef struct {
char *c00;
char *c01;
char *c02;
} CURRENCIES;
CURRENCIES *currencies = {
c00 = "EUR";
c01 = "USD";
c02 = "AUD";
}
char **_chrCur = (char**)currencies;
char **_chrCurLast = _chrCur + sizeof(CURRENCIES) / sizeof(char*);
for (; _chrCur<_chrCurLast; _chrCur+=1) {
|
|
|
Re: Array of strings and size
[Re: txesmi]
#472368
04/23/18 10:45
04/23/18 10:45
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
|
|
|
|