Array of strings and size

Posted By: sdelatorre

Array of strings and size - 04/22/18 21:01


Hello,

I need array of strings but it's not posible Access to it,

char *p[3]={"EUR","USD","AUD"}

when I Access p[1] it's null but I can do : p[1]="EUR" and shows the correct element. I testes for string [] but its no correct..


I can't get the size ...

Sorry I want only an array of strings and I want get the size for a loop

p[3]={"EUR","USD","AUD"}
while i<size(p) do

how can I do it ?
Posted By: 3run

Re: Array of strings and size - 04/22/18 21:58

Hi!

This might not be the best solution, but this is how I handle such stuff:
Code:
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:
Code:
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..
Posted By: sdelatorre

Re: Array of strings and size - 04/23/18 08:47


Thanks, but

I want only an array of strings and the size of this string....all that for this?
Posted By: 3run

Re: Array of strings and size - 04/23/18 09:47

Originally Posted By: sdelatorre

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'.
Code:
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);
		
	}
	
}

Posted By: txesmi

Re: Array of strings and size - 04/23/18 10:36

Do you like risk?

Code:
#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);



grin

edited_______________

or with no index at all

Code:
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.

Code:
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) {

Posted By: 3run

Re: Array of strings and size - 04/23/18 10:45

grin
© 2024 lite-C Forums