I have a struct with letters inside and I want to give the letters to an array so i can use it afterwards. It works if i would just print the letters.
Top (struct):
#include "stdafx.h"
typedef struct { char* code; char* letter;}morse_tabelle;
morse_tabelle tabelle[] = {
{ ".-", "A" },
{ "-...", "B" },
{ " -.-. ", "C" },
{ "-..", "D" },
{ ".", "E" },
{ "..-.", "F" },
{ "--.", "G" },
{ "....", "H" },
{ "..", "I" },
{ ".---", "J" },
{ "-.-", "K" },
{ ".-..", "L" },
{ "--", "M" },
{ "-.", "N" },
{ "---", "O" },
{ ".--.", "P" },
{ "--.-", "Q" },
{ ".-.", "R" },
{ "...", "S" },
{ "-", "T" },
{ "..-", "U" },
{ "...-", "V" },
{ ".--", "W" },
{ "-..-", "X" },
{ "-.--", "Y" },
{ "--..", "Z" },
{ "-----", "0" },
{ ".----", "1" },
{ "..---", "2" },
{ "...--", "3" },
{ "....-", "4" },
{ ".....", "5" },
{ "-....", "6" },
{ "--...", "7" },
{ "---..", "8" },
{"----.", "9" },
{ "/", " " },
};
For the arraysize:
#define ARR_SIZE(x) (sizeof(x)/sizeof((x)[0]))
Function:
void morse_to_text(void)
{
char input[100];
char* morse_letter;
int j = 0;
char* translation[50];
printf_s("\n\nput in Morsecode:\n\n");
fgets(input, 100, stdin);
input[strlen(input)-1] = '\0';
morse_letter = strtok(input, " ");
while (morse_letter)
{
for (int i = 0; i < ARR_SIZE(tabelle); i++)
{
if (!strcmp(morse_letter, tabelle[i].code))
{
translation[j] = tabelle[i].letter;
j++;
/*printf_s("%s", tabelle[i].letter);*/ //This works
}
}
morse_letter = strtok(NULL, " ");
}
/*output*/
printf_s("\n\n---------------\n\n");
for (int i = 0; i <= strlen(translation[50]); i++){
printf("%s", translation[i]);
}
};
It sort of works if i change the char* letter to char letter inside of the struct. but then i get a buffer overrun.
The question again: how can i store strings inside an array.
strlen(translation[50])?for (int i = 0; i <= strlen(translation[50]); i++)->for (int i = 0; i < j; i++)breakafterj++inforloop for efficiency