当给定const char *作为第一个参数时,C标准库函数`strchr`如何返回指向非const的指针? [英] How come C standard library function `strchr` returns pointer to non-const, when given `const char *` as first argument?

查看:205
本文介绍了当给定const char *作为第一个参数时,C标准库函数`strchr`如何返回指向非const的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gcc / g ++编译给定的代码示例成功。对于 strchr 调用没有错误,这显然会将 const char * 赋给 char *



我发现 strchr 被声明为 char * strchr(const char *,int)在两个不同的来源 pubs.opengroup.org cplusplus.com



如果 strchr 被实现为抛弃const,那么为什么这样呢?


$ b $如果目标是提供可以在 char * const char * 字符串上运行的函数 - 它可以已使用两个不同的函数名称实现。



您可以对此进行更全面的解释。

  #include< string.h> 

int main(){
const char * str =Sample string;
char * ptr;

// ptr = str; //错误:丢弃const限定符 - 在gcc和g ++上都是
pch = strchr(str,'S'); //成功分配< const char *>到< char *>

* pch ='4'; //运行时错误:分段错误

return 0;



$ b在Win7上使用MSYS2 / mingw-w64 gcc_v5.3.0和TDM- gcc32 v5.1.0。

解决方案当您调用 strchr code> char * 你需要一个 char * 回来,这样你就不需要对结果进行强制转换。 C不支持函数重载,因此使用相同的 strchr 函数来搜索 char * const char * 。它在所有情况下都返回一个非const指针。如果你使用 const char * 来调用它,它基本上抛弃了 const ,因此调用者有责任使用它安全地(例如立即将结果分配给 const char * ,以避免任何不安全地使用返回的指针)。


如果目标是提供对char *和const char *字符串都可用的函数 - 它可以使用两个不同的函数名称来实现。


< blockquote>

是的,它可能是,但它不是这样做的。原始版本的C根本不支持 const ,所以程序员总是有责任避免尝试修改字符串文字。如果使用了不同的函数名称,那么它会安全地破坏使用 strchr 的现有代码。如果像这样的传统C代码突然停止编译,它会减缓采用新的 const 关键字:

  char arr [] =foo bar; 
* strchr(arr,'r')='z';

C精神是它不会阻止你做不安全的事情,它是您的工作,以编写正确的代码。



在C ++中 strchr 被重载以保持常量:

  char * strchr(char *,int); 
const char * strchr(const char *,int);

这意味着它会自动执行正确的操作,并且用<$ $写入不安全的代码更加困难c $ c> strchr 。


Compilation of given code sample with gcc/g++ succeeds. There is no error for strchr call, which obviously assignes const char * to char *.

I've found strchr is declared as char * strchr(const char *, int) on two different sources pubs.opengroup.org and cplusplus.com

If strchr is implemented as to cast away constness, then why's that so?

If goal was to provide function which works both on char * and const char * strings - it could have been implemented using two different function names.

Can you give more thorough explanation on this.

#include <string.h>

int main () {
    const char *str = "Sample string";
    char * ptr;

    //ptr = str;            //  Error: discards const qualifier - both on gcc and g++
    pch = strchr(str,'S');  //  Succeeds in assigning <const char *> to <char *>

    *pch = '4';             //  Runtime error: segmentation fault

    return 0;
}

Tried it on Win7 using MSYS2/mingw-w64 gcc_v5.3.0 and TDM-gcc32 v5.1.0 .

解决方案

When you call strchr with a char* you want a char* back, so that you don't need to do a cast on the result. C doesn't support function overloading, so the same strchr function is used for searching in a char* and in a const char*. It returns a non-const pointer in all cases. If you call it with a const char* it is basically casting away the const so it is the callers responsibility to use it safely (e.g. by assigning the result to a const char* immediately, to avoid any unsafe use of the returned pointer).

If goal was to provide function which works both on char * and const char * strings - it could have been implemented using two different function names.

Yes, it could have been, but it isn't done like that. The original version of C didn't support const at all, so it was always the programmer's responsibility to avoid trying to modify string literals. If different function names were used then it would have broken existing code that used strchr safely. It would have slowed the adoption of the new const keyword if traditional C code like this suddenly stopped compiling:

char arr[] = "foo bar";
*strchr(arr, 'r') = 'z';

The "spirit of C" is that it doesn't try to stop you doing unsafe things, it's your job to write correct code.

In C++ strchr is overloaded to preserve constness:

char* strchr(char*, int);
const char* strchr(const char*, int);

This means it automatically does the right thing, and it's much harder to accidentally write unsafe code with strchr.

这篇关于当给定const char *作为第一个参数时,C标准库函数`strchr`如何返回指向非const的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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