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!

3
  • 1
    This is not a minimal working example. You could reduce it to reading just one or two fields, and you have not defined struct instructionEmMnemonico. Commented Jun 9, 2019 at 2:42
  • Sorry, I will pay more attention on this next time! :) Commented Jun 9, 2019 at 10:55
  • while(!feof(arquivo)) { <<-- seen this one before... stackoverflow.com/q/5431941/905902 Commented Jun 9, 2019 at 10:57

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.