1

It's my first post here in StackOverflow! I'm trying to write a code that reads an assembly code and stores it in an array of structs. I implemented a function that does this. Inside the function, it seems the values have correctly been passed to the array of structs, as I tried to see the values in the structs with some printf. But when I try to see the same values outside the function (so, I used some printf in the main function for this), the values printed are absolutely wrong!

Obs: The logic of the code is not complete, I'm just trying to solve the file reading problem for now. Obs2: the .txt file I'm trying to read is:

oi MOV s1,s2

tchau ADD s3,s4

1 sub s5,s6

void loader(struct instructionEmMnemonico mnemonicoInstru[500]) {
    FILE* arquivo;
    int i = 0, j = 0;
    char letra = '0';
    int endline = 0;
    int mnemonico = 0;
    char line[90];
    char *token, *result;

    arquivo = fopen("teste.txt", "r");
        if(arquivo == NULL) {
            printf("Arquivo nao pode ser aberto\n");
        }
        while(!feof(arquivo)) {
            fgets(line, 90, arquivo);
            printf("%c-----------\n",line[0]);
            if(line[0] != ' ' || line[0] != '\n' || line[0] != '\t') {
                token = strtok(line,", \n\t");
                mnemonicoInstru[i].label = token;
                printf("%s\n",mnemonicoInstru[i].label);
                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].mnemonico = token;
                printf("%s\n",mnemonicoInstru[i].mnemonico);
                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].operando1 = token;
                printf("%s\n",mnemonicoInstru[i].operando1);
                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].operando2 = token;
                printf("%s\n",mnemonicoInstru[i].operando2);
            } else {
                token = strtok(line,", \n\t");
                mnemonicoInstru[i].label = token;
                printf("%s\n",mnemonicoInstru[i].label);
                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].mnemonico = token;
                printf("%s\n",mnemonicoInstru[i].mnemonico);

                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].operando1 = token;
                printf("%s\n",mnemonicoInstru[i].operando1);

                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].operando2 = token;
                printf("%s\n",mnemonicoInstru[i].operando2);

                token = strtok(NULL,", \n\t");
                mnemonicoInstru[i].operando3 = token;
                printf("%s\n",mnemonicoInstru[i].operando3);
            }
            i++;
        }
    fclose(arquivo);
}


int main() {
    struct instructionEmMnemonico programa[500];

    loader(programa);
    printf("-----\n");
    printf("%c--------\n",programa[2].label[0]);
    printf("%s---\n",programa[2].mnemonico);
    printf("%s--\n",programa[2].operando1);
    printf("%s-\n",programa[2].operando2);
return 0;
}

In the printf inside the function "loader", the results are correct, but the printf in the main function doesn't make any sense!

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
Rafael Higa
  • 655
  • 1
  • 8
  • 17

1 Answers1

3

It is because you are assigning pointers. mnemonicoInstru[i].label = token;

since token is pointer to local variable line you have undefined behavior when you access it outside the loader function.

Copy the actual contents.

mnemonicoInstru[i].label = strdup(token);

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44