C 为什么';斯特罗德的工作方式是否正确?
我正在使用GNU GCC编译器在代码块编辑器中进行编码。我尝试使用函数C 为什么';斯特罗德的工作方式是否正确?,c,string,C,String,我正在使用GNU GCC编译器在代码块编辑器中进行编码。我尝试使用函数strod,其原型如下: double strtod(常量字符*a,字符**b) 如果我使用以下代码: #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { char *a; a="99.5HELLO"; char *b; printf("%.1lf\n%s", strto
strod
,其原型如下:
double strtod(常量字符*a,字符**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
@
发生了什么事?哪里出错了?子表达式的求值顺序未指定,因此可能会先求值最后一个函数参数,然后读取未初始化的值b
,这是未定义的行为
排序评估:
const char *a = "99.5HELLO";
char *b;
double d = strtod(a, &b);
printf("%.1f\n%s", d, b);
注意:1)字符串文本是不可变的;通过const char*
引用它们。2) %lf
与%f
相同,l
对浮点转换无效(不要与l
混淆!)。
const char *a = "99.5HELLO";
char *b;
double d = strtod(a, &b);
printf("%.1f\n%s", d, b);