I am finding trouble while I was writing a program in C. I have defined a structure:
typedef struct {
char* alphabet; /* Defines the supported characters by the encryption algorithm */
char* symbols; /* Defines the symbols used to encrypt a message */
char* dictionary; /* Defines the translation for all characters defined in alphabet */
char* transposition; /* Defines the transposition key used in the second encryption step */
} adfgx_machine;
I also have created a method to create an instance of this structure:
adfgx_machine* am_create(char* alphabet, char* symbols, char* dictionary, char* transposition) {
adfgx_machine machine;
if(strlen(alphabet)*2!=strlen(dictionary)){
printf("s", "Het aantal karakters in de dictionary moet dubbel zoveel zijn als het antal karakters in alphabet!");
exit(1);
}
machine.alphabet=alphabet;
machine.symbols=symbols;
machine.dictionary=dictionary;
machine.transposition=transposition;
return &machine;
}
Now I am trying to print for example the alphabet of the structure if such a structure is given, but my program always crashes. I have already tried the [dot] operator but that one doesn't work either. Here's my code:
void am_create_dictionary(adfgx_machine* am) {
printf("%s",am->alphabet);
}
This is my main method:
int main(int argc, char* argv []) {
adfgx_machine* mach = am_create("azert","azert","azertazert","azert");
am_create_dictionary(mach);
return 0;
}
So if i replace am->alphabet with am.alphabet it doesn't work either. What am I doing wrong?
UPDATE:
if I don't use my method but print it directly in the main method, it does work?! So then my main method becomes:
int main(int argc, char* argv []) {
adfgx_machine* mach = am_create("azert","azert","azertazert","azert");
printf("%s",mach->alphabet);
return 0;
}