i am working in c (i am really a beginner at coding), i want to write a functions tha allows me to translate the text i write in the prompt in morse. So i am trying by define my variables from the letter of the elphabet to the respective morse code but i always get a warning of multi-character and don't know how to move on.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
//funzione per scrivere codice morse
int main()
{
char a='.-';
printf("%c",a);
return 0;
}
CodePudding user response:
What @Barmar told you is correct:
char a[] = ".-";`
You also tell us that you were trying to defining the mapping from letters to morse code. This could be using a struct along with a lookup
function to lookup each letter:
#include <stdio.h>
struct map {
char letter;
char *morse;
};
// return morse string for letter, or NULL if not found
const char *lookup(const struct map *m, const char letter) {
for(unsigned i = 0; m[i].letter; i ) {
if(m[i].letter == letter)
return m[i].morse;
}
return NULL;
}
int main() {
struct map m[] = {
{ 'A', (char *) ".-" },
{ 'B', (char *) "-..." },
// ...
{ '\0', NULL }
};
printf("%c => %s\n", 'A', lookup(m, 'A'));
}
Once you get a little more experience you can replace the lookup()
function with lfind()
or if the map is sorted by letter bsearch()
.