tags:

views:

164

answers:

4

I have this look up table :

char *table[ascii][morse];

where ascii is an int (representing an ascii letter) and morse is a string of length 4 + 1 ( to add the null). It is a look up table in a way where you ask for letter 0 = a and it should return the morse represntation of it.

if I have a ..-. (for example) in a file. and i want to add this to the look up table where index (ascii) = 0 how do i do that. Also, how do i print all the information in the look up table as : 0 > ..-. etc.

If you don't get the problem please tell me how i can explain more :)

The code i have right now to put chars in the look table is (and it is wrong) :

void ascii-morse (Lookuptable *table, char ascii, char *morsepassed) {
  int index = ascii - 'a';
  char copy[5];
  strcpy(copy, morsepassed); // we need to copy the morse passed
  table->table[index][5] = copy;
}

Please note that Lookuptable is a struct and that i have to have the same variables passed that way to my function

+1  A: 

strcpy(table->table[(int)ascii], morsepassed) would seem to be the way to do it (what's "index" supposed to be set to? maybe to (int)ascii? I don't see the point).

Alex Martelli
A: 

The line

table->table[index][5] = copy;

is wrong -- you don't copy strings that way in C. Does that help? You also forgot to initialize 'index'.

Rob Pelletier
A: 

Your variable "copy" is on the stack, it will go away when the function exits and your pointer will be pointing at garbage.

You need to use malloc and free to allocate your morse code string, or used a fixed-size character array and copy directly into that.

Dan Olson
+3  A: 

The definition of your table is slightly wrong. You have this:

char *table[ascii][morse];

Which (presuming "ascii" is 26, and "morse" is 5) is a table of 26 * 5 pointers-to-char. You actually just want 26 * 5 chars (5 chars for each letter of the alphabet):

char table[ascii][morse];

Then you can change your function to use this to copy the value into the table:

strcpy(table->table[index], morsepassed);
caf