0

I have some code that is meant to read the first 3 characters from a character array that is read from a file, it was working then without changing anything is stopped working. The 'command' char array use to hold "and" but now often holds "add▒" and sometimes "and0" but I've only declared it to be 3 long yet it still manages to hold more. Is there some context I am missing?

//ORIGINALLY THIS WORKED

    for (i = 0; i < 3; i++){
        command[i] = line[i];
    }

    /*Interpret AND or ADD or JMP */
    if (strcmp(command,"and") == 0){
        hexLine[0] = changeHex(5);
    }else if (strcmp(command,"add") == 0){
            hexLine[0] = changeHex(1);
    }else if (strcmp(command,"jmp") == 0){
            hexLine[0] = changeHex(12);
    }
    printf("%s", command);

//AND NOW THIS DOESNT WORK

for (i = 0; i < 3; i++){
    command[i] = line[i];
}

/*Interpret AND or ADD or JMP */

if (strcmp(command,"and") == 0){
    hexLine[0] = changeHex(5);
}else if (strcmp(command,"add") == 0){
    hexLine[0] = changeHex(1);
}else if (strcmp(command,"jmp") == 0){
    hexLine[0] = changeHex(12);
}else if (strcmp(command,"ld ") == 0){
    hexLine[0] = changeHex(2);
}
printf("%s", command);
10
  • 1
    What exactly makes you think there's a fourth character in command? Commented Jun 1, 2014 at 22:06
  • I printf("%s", command); Commented Jun 1, 2014 at 22:10
  • Your for loop copies 3 characters but doesn't zero-terminate command. So strcmp won't behave the way you want. Put command[i] = 0; after your for loop. Commented Jun 1, 2014 at 22:12
  • Don't forget the null-character. See cplusplus.com/reference/cstring/strcmp Commented Jun 1, 2014 at 22:13
  • 1
    If your question has been answered, please don't edit your question to reflect that fact. Instead accept the answer you think is correct. Commented Jun 1, 2014 at 22:34

2 Answers 2

1

Your for loop copies 3 characters but doesn't zero-terminate command. So strcmp won't behave the way you want. Put command[i] = 0; after your for loop.

for (i = 0; i < 3; i++){
    command[i] = line[i];
}

command[i] = `\0';

As @Klaus points out in his comment: the above for loop assumes that you always have 3 valid characters to copy over. And, of course, command must be an array of at least 4 characters.

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

5 Comments

Thank you so much, this fixed the error even with only 3 spaces.
stepping on a 4th entry in command when command is only defined as three chars will corrupt the stack. If you going to use this method, expand the size of command to 4 chars (still only read 3 chars.)
@user3629249 of course. Op's originsl code doesn't show the declaration of command but I have edited to make that clear in my response.
Generally, you should modify the code to accommodate the data rather than hacking data representation to accommodate the code.
@user3629249 indeed. But given that there is little information available for the OP's context, it's unclear whether the data is actually being "hacked" in this case.
0

you need to put terminator character \0 in the end of the command char array

for (i = 0; i < 3; i++){
command[i] = line[i];
}

command[i + 1] = '\0';

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.