- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
效果很好。
#include <stdio.h>
#include <stdlib.h>
int main(void){
char number[]= "a123.45", *strtod_eptr;
double num;
num=strtod(number, &strtod_eptr);
if (strtod_eptr == number){
printf("Error: no number found.\n");
}
else{ printf("%f\n", num+7);
}
return 0;
}
不起作用。 strtod() 中的第二个参数改变了类型。
#include <stdio.h>
#include <stdlib.h>
int main(void){
char number[]= "a123.45", **strtod_epptr;
double num;
num=strtod(number, strtod_epptr);
if (*strtod_epptr == number){
printf("Error: no number found.\n");
}
else{
printf("%f\n", num+7);
}
return 0;
}
编译器警告未初始化的 strtod_epptr 但没有编译错误。
strol_test.c:7:5: warning: ‘strtod_epptr’ is used uninitialized in this function [-Wuninitialized]
程序在 if() 语句处崩溃(段错误)。
编译器命令(在两种情况下):
gcc -Wall -pedantic -o "strol_test" "strol_test.c"
为什么会这样?为什么 gcc 提示未初始化的 **strtod_epptr 而完全没问题(类似地未初始化?) *strtod_eptr ?取消引用 **strtod_epptr 时出了什么问题?据我所知:
char *ptr;
...strtod(...,&ptr)
应该是一样的
char **ptr;
...strtod(...,ptr)
但显然不是。我错过了什么?
最佳答案
想想内存。
当您编写 char *str
时,您要求编译器为 char 指针分配内存。现在,当您将 str
的地址发送到函数时,如 &str
str 中的值可以更改。
现在反过来。
当您编写 char **str
时,您要求编译器为指向 char 指针的指针分配内存。这意味着没有为 char 指针分配内存。如果您现在取消引用该指针,它将指向任何有意义的内容。
现在,您将 str
传递给 strtod()
。该函数现在执行 *str = something()
。但是那个地方在内存中不存在。该存储桶未创建和分配。
以后,总是传递一个已经存在的变量的地址。如果不是,则函数无法更新变量...
关于c: strtod:双指针与对单指针的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591705/
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 7 年前。 我的 strtod() 有问题似乎添加了一些数字。我正在阅读 2\t5241
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 7 年前。 我有一个有趣的程序,我想给出一个参数,以便它将触发最后一个 else 条件来
我正在学习 Bjarne Stroustrup 的《编程 - 原则与实践》,一个相当简单的练习让我感到难过(这只是第 11 部分中的第 2 部分)。 #include "../../std_lib_f
我正在学习 C++,并且正在完成一些示例练习。特别是这个接受命令行参数,然后对它们进行数学运算,这样如果我输入 ./ex1 sum 1.0 2.0 3.0 它应该对数字求和。我的代码是这样的: int
我正在使用 strtod() 将字符串转换为十进制。由于我需要为不正确的输入/无效字符抛出错误,所以我别无选择。 然而,问题是 strtod() 受语言环境影响。所以一个'.'当程序在不同的语言环境中
我有一个缓冲区,其中有“随机”数字,用逗号分隔。例如: “1589.3,12478.359,485.39971”等 我需要检查小数部分和整数部分的长度。 我做了一些研究,然后决定在我的缓冲区上使用 s
我不想不必要地重新发明轮子,但我一直在寻找 strtod 的功能,但有一个基本参数 (2,8,10,16)。 (我知道 strtoul 允许一个基本参数,但我正在寻找返回类型 double)。任何正确
在 C++ (Visual C++ 2010) 中使用 strtod 函数将字符串转换为 double 时,我想检测下溢。尽管我根据 strtod 的文档做了以下代码,但它并没有像我预期的那样工作:
效果很好。 #include #include int main(void){ char number[]= "a123.45", *strtod_eptr; double num;
简单的问题:数字 1.15507e-173 的 double 正确位表示是什么?完整问题:如何确定这个数字的正确解析? 背景:我的问题来自 this answer它显示了来自三个不同解析器的两个不同的
如果我想将 char 数组中的前 3 个字符解析为 double,忽略后面的字符,我真的需要这样做吗? int main() { const char a[] = "1.23"; char
我有以下代码: int check_for_non_number(char *input, int lineNum) { errno = 0; char *endptr; pr
我目前正在为我大学的实验室练习开发一个银行终端程序。 让我大吃一惊的是一个函数,它应该接受用户输入的转账金额,检查它是否符合所有要求,如果符合,则返回提供给程序的值。 我们的导师对所有确保输入安全的方
我正在使用 GNU GCC 编译器在代码块编辑器中编码。我尝试使用以下原型(prototype)的函数 strtod: double strtod(const char *a, char **b);
我的 C 项目遇到了一些麻烦。我使用 fgets(line, 1024, stdin) 读取了一行。在该行中,应该有 4 个用空格分隔的参数,如果没有,程序应该写一个警告。像这样的事情: “1f 2
我一直致力于处理如下所示的输入文件的项目: A 0.0345 B 0.3945 ... Z 0.2055 基本上,我正在阅读每一行,在阅读完该行之后,我想仅从字符串中拉出 double 值。我正在尝试
所以我有这个 Java 程序,我用它来处理数 TB 的数据。性能是一个问题。 我分析了该应用程序,所有内存分配的很大一部分以及 CPU 时间的很大一部分来自执行一个简单的操作: 我有一个 ASCII
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 #include ... cin >> str; errno = 0 ; double d = strtod(str
我正在将部分 C++ 程序转换为 Python,但我在替换 C 函数时遇到了一些问题 strtod .我正在处理的字符串由简单的数学方程式组成,例如“KM/1000.0”。问题是常量和数字混合在一起,
我有一个库需要解析总是使用点“.”的双数。作为小数分隔符。不幸的是,对于这种情况,strtod() 尊重可能使用不同分隔符的语言环境,因此解析可能会失败。我不能 setlocale() - 它不是线程
我是一名优秀的程序员,十分优秀!