I have the code (simplified a little for example)
char *op = CURRENT_PARSED_STRING.operand;
if(op){
printf("%s\n", op);
char q[] = "12h";
int a = atoi(q);
printf("%d %s\n", a, op);
}
CURRENT_PARSED_STRING is an instance of a structure that looks like this
typedef struct ASSEMBLY_LINE{
char *marker;
char *operator;
char *operand;
char *comment;
} ASSEMBLY_LINE;
when executing the line
int a = atoi(q);
CURRENT_PARSED_STRING.operand changes, and with it the op. In this case, the correct value is written in a. Here is the result of the execution:
12h //op content before
12 Q4R //correct value and garbage
Changing atoi() to strtol() does not work. This code is needed to illustrate the problem, in fact, I need to convert to a number exactly op.
Edit: yes, I made a mistake with the format specifier, but that was not the reason. I checked my program in the debugger, and it showed that CURRENT_PARSED_STRING is completely fine, right up to the line with atoi. I included all the flags when compiling, but that didn't work either. I don't really understand how to make the question cleaner because CURRENT_PARSED_STRING appears in different parts of the code and for full reproducibility I would have to attach almost all the code. But I see that the structure looks intact - the lines end with a null character, unused fields = NULL.
The problem is most likely an incorrect format string. The variable
a
is anint
so you want to use the format%d
(%ld
is forlong
):