我正在用GNU GCC编译器编写代码块编辑器,我尝试使用以下原型的函数strtod:
double strtod(const char *a, char **b);
如果我使用以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char *a;
a="99.5HELLO";
char *b;
printf("%.1lf\n%s", strtod(a, &b), b);
return 0;
}我希望控制台终端在运行代码后表示如下内容:
99.5
HELLO但我真正得到的是一些奇怪的东西:
99.5
@发生什么事了呢?我在哪里犯了个错误?
发布于 2015-12-12 11:48:42
子表达式的求值顺序未指定,因此可以先计算最后一个函数参数,最后读取未初始化的值b,这是未定义的行为。
按顺序排列评价结果:
const char *a = "99.5HELLO";
char *b;
double d = strtod(a, &b);
printf("%.1f\n%s", d, b);https://stackoverflow.com/questions/34239453
复制相似问题