为什么endptr参数strtof和strtod将指向一个非const字符指针? [英] Why is the endptr parameter to strtof and strtod a pointer to a non-const char pointer?

查看:182
本文介绍了为什么endptr参数strtof和strtod将指向一个非const字符指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准C库函数 strtof 的strtod 具有以下特征:

The standard C library functions strtof and strtod have the following signatures:

float strtof(const char *str, char **endptr);
double strtod(const char *str, char **endptr); 

他们每个分解输入字符串, STR ,分为三个部分:


  1. 空白的初始,可能是空的,序列

  2. 的重新present一个浮点值
  3. 字符的目标序列
  4. A后序是无法识别的(并且不影响转换)的字符。

  1. An initial, possibly-empty, sequence of whitespace
  2. A "subject sequence" of characters that represent a floating-point value
  3. A "trailing sequence" of characters that are unrecognized (and which do not affect the conversion).

如果 endptr 不是 NULL ,那么 * endptr 设置为指针立即最后的字符,这是转换的一部分(换言之,尾随序列的开始)之后的字符。

If endptr is not NULL, then *endptr is set to a pointer to the character immediately following the last character that was part of the conversion (in other words, the start of the trailing sequence).

我很纳闷:为什么 endptr ,然后,一个指针的非 - 常量 字符指针?是不是 * endptr A指针变成常量 字符字符串(输入字符串 STR )?

I am wondering: why is endptr, then, a pointer to a non-const char pointer? Isn't *endptr a pointer into a const char string (the input string str)?

推荐答案

原因很简单,实用性。 的char * 可以自动转换为为const char * ,但的char ** 不能自动转换为通过调用函数中使用为const char ** ,实际的类型指针(其地址被传递)的更可能是的char * 为const char * 。之所以这样自动转换是不可能的是,有可用于去除常量一个非显而易见的方式通过几个步骤,其中每个步骤看起来完全有效的鉴定和纠正本身。史蒂夫·杰索普已经在发表了一个例子:

The reason is simply usability. char * can automatically convert to const char *, but char ** cannot automatically convert to const char **, and the actual type of the pointer (whose address gets passed) used by the calling function is much more likely to be char * than const char *. The reason this automatic conversion is not possible is that there is a non-obvious way it can be used to remove the const qualification through several steps, where each step looks perfectly valid and correct in and of itself. Steve Jessop has provided an example in the comments:

如果你能自动转换的char ** 为const char ** ,那么你可以做

if you could automatically convert char** to const char**, then you could do

char *p;
char **pp = &p;
const char** cp = pp;
*cp = (const char*) "hello";
*p = 'j';.

有关const的安​​全,这些线路之一必须是非法的,因为其他人都是完全正常的操作,它必须是 CP = PP;

For const-safety, one of those lines must be illegal, and since the others are all perfectly normal operations, it has to be cp = pp;

<击>更好的方法本来是要定义这些函数取无效* 的char **的。无论的char ** 为const char ** 可以自动转换为无效* (将灾区文本实际上是一个非常糟糕的主意。它不仅prevent任何类型的检查,但ç实际上禁止类型的对象的char * 为const char * 别名)。此外,这些功能可能已经采取了 ptrdiff_t的* 为size_t * 参数在其中存储的偏移的结束,而不是一个指针。这往往是比较有用呢。

A much better approach would have been to define these functions to take void * in place of char **. Both char ** and const char ** can automatically convert to void *. (The stricken text was actually a very bad idea; not only does it prevent any type checking, but C actually forbids objects of type char * and const char * to alias.) Alternatively, these functions could have taken a ptrdiff_t * or size_t * argument in which to store the offset of the end, rather than a pointer to it. This is often more useful anyway.

如果你喜欢后一种方式,随便写身边标准库函数这样的包装,并打电话给你的包装,这样才能让你的code 常量 -clean和免费铸。

If you like the latter approach, feel free to write such a wrapper around the standard library functions and call your wrapper, so as to keep the rest of your code const-clean and cast-free.

这篇关于为什么endptr参数strtof和strtod将指向一个非const字符指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆