-1

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;
}
Lennart
  • 383
  • 4
  • 16
  • There are an enormous number of duplicates of this problem. https://www.google.com/webhp#q=stack+overflow+returning+stack+variable+pointer http://stackoverflow.com/search?q=returning+local+variable+pointer+%5Bc%5D – Bill Lynch Oct 14 '14 at 17:07

2 Answers2

3

The main problem is that you allocate return value on stack. It will be likely to be overriden. You better to use malloc()

adfgx_machine* am_create(char* alphabet, char* symbols, char* dictionary, char* transposition) {
    adfgx_machine* machine = malloc(sizeof(*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;
}
Eldar Dordzhiev
  • 5,105
  • 2
  • 22
  • 26
2

You are creating the structure variable adfgx_machine machine; inside the function am_create(), so it becomes the local variable for that function. And, when the function exits, all its local variables are destroyed, along with machine.

To accomplish what you want, you can allocate the memory for the variable machine dynamically, using malloc() or calloc()

Haris
  • 12,120
  • 6
  • 43
  • 70