I am implementing Morse code using C and ran into some fundamentals.
Basic idea is I have 2-dimensional character array for A through Z. The first element of each array is an alphabet followed by corresponding Morse code. Program will receive a character which will be parsed through entire array once the match is found I will spit out the Morse code. I am able to run this program using help of simple array manipulation but I an running into issue with compact array.
The array is defined as below,
char *morseKey[37] = {
{"A.-"},
{"B-..."},
{"C-.-."},
{"D-.."},
{"E."},
{"F..-."},
{"G--."},
{"H...."},
{"I.."},
{"J.---"},
{"L.-.."},
{"K-.-"},
{"L.-.."},
{"M--"},
{"N-."},
{"O---"},
{"P.--."},
{"Q--.-"},
{"R.-."},
{"S..."},
{"T-"},
{"U..-"},
{"V...-"},
{"W.--"},
{"X-..-"},
{"Y-.--"},
{"Z--.."}
};
Regular 2D array implementation ,
/*
Find a match for each character from nameCode string that need to be
transformed to Morse Code. Once the match is found then based on the
Morse code flash the LED for dash and dot.
*/
for(char *cp_nameCode = &nameCode[0]; *cp_nameCode != '\0'; cp_nameCode++)
{
charLoc = GetMorseCode(morseKey, first, last, *cp_nameCode);
for(int col = 1; cp_morseKey[charLoc][col] != '\0'; col++)
{
cp_morseKey[charLoc][col] == '.' ? dot() : dash();
}
}
Now my approach is to declare a pointer to point to this array as below,
char **cp = morseCode;
and then with the help of *cp and **cp access the columns and rows sequentially. But it does not work.
Alternatively, I tried declaring,
char **cp = morseCode;
char *cp_row = *morseCode;
and then accessing as below,
while(*cp != '\0')
{
while(*cp_row != '\0')
{
//printf("%c\n", **cp++);
printf("%c\n", *cp_row++);
}
cp_row = *((++*cp));
//cp++;
}
But the code does not go beyond the first row. I feel there is still gap in my pointer knowledge.
I will appreciate any pointers to resolve this bottleneck.