我有代码(例如稍微简化一下)
char *op = CURRENT_PARSED_STRING.operand;
if(op){
printf("%s\n", op);
char q[] = "12h";
int a = atoi(q);
printf("%ld %s\n", a, op);
}
CURRENT_PARSED_STRING 是一个结构实例,如下所示
typedef struct ASSEMBLY_LINE{
char *marker;
char *operator;
char *operand;
char *comment;
} ASSEMBLY_LINE;
执行该行时
int a = atoi(q);
CURRENT_PARSED_STRING.operand 发生变化,操作也随之变化。在这种情况下,正确的值被写入a中。 执行结果如下:
12h //op content before
12 Q4R //correct value and garbage
将 atoi() 更改为 strtol() 不起作用。需要这段代码来说明问题,事实上,我需要精确地转换为数字op
问题很可能是格式字符串不正确。变量
a
是一个 int
,因此您要使用格式 %d
(%ld
代表 long
):
printf("%d %s\n", a, op);