English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here's a sample of a program:

char cur[6][20]={"Euro ", "YEN ", "Franc", "ZWD", "DOP", "Exit"}; //five currencies types plus exit option (6)
float rates[5] ={1.3859, 114.9750, 0.8424, 0.00003333, 0.02988}; // five (5)exchange rates


What would the (20) relate to if we are doing currency conversion? Why should the number (20) be used in this example if the conversion is hard-coded why not any other number?

2007-09-17 22:18:55 · 4 answers · asked by tiera29 2 in Computers & Internet Programming & Design

4 answers

The cur[6] relate to the five currency options (+Exit) in the array, and [20] is the size of the string in each of the arrays, so the longest currency /option name is "Franc" that is at cur[2]. So there is a 'wasted space' for minimum 14 bytes (1 byte required for string terminating null character \x0) for each array from [0] to [6].
Therefore your declaration may be like char cur[6][6] = {"Euro".......};

2007-09-17 23:43:50 · answer #1 · answered by Irfan 1 · 1 1

Hi,
Of course, 20 is the maximum length of the string that can be stored in cur[6][20] array. But there is wastage of space. Only "Franc" is using max space, i.e., 5 charachter. Still there is wastage of 15 characters.

Instead of above u could declare a character array of pointers that would save space without writing any fixed size (in this case 20). Still u can save string of any length.

Alternative code of ur code is:
char *cur[6]={"Euro","Yen","Franc","ZWD","DOP","Exit"};
u can access any of the elements by writing cur[4], cur[0] etc.

2007-09-18 00:24:32 · answer #2 · answered by iqbal 4 · 1 1

u can use 6 in place of 20 try it
20 is used for length of maximun characters in char variable

2007-09-17 22:48:53 · answer #3 · answered by i_am_the_next_best_one 5 · 1 0

Hi,
I am no expert but i think it's the "Text String Length" in the array,

2007-09-17 22:30:18 · answer #4 · answered by Anonymous · 1 1

fedest.com, questions and answers