C语言 警告:从指针目标类型中丢弃“const”限定符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37160873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:24:45  来源:igfitidea点击:

warning: discards 'const' qualifiers from pointer target type

c

提问by Ummair

Doesn't const char *smeans that "s is a pointer which is pointing towards a constant char " then why it is giving me this warning? I am not trying to change values.

并不 const char *s意味着“s 是一个指向常量字符的指针”那么它为什么给我这个警告?我不是要改变价值观。

In first function warning is return discards 'const' qualifiers from pointer target type.

在第一个功能警告是return discards 'const' qualifiers from pointer target type.

and in second warning is assignment discards 'const' qualifiers from pointer target type.

第二个警告是assignment discards 'const' qualifiers from pointer target type.

I was trying to make library functions which are defined in string.h, and also tell me how to correct it.

我试图制作在 中定义的库函数string.h,并告诉我如何更正它。

char *my_strchr( const char *s, int c )
{
    for(;*s!='##代码##';s++)
       if(*s==c)
          return s; // warning

    return 0;
}



char *my_strpbrk( const char *s1, const char *s2 )
{
    char *s2ptr;

    for(;*s1!='##代码##';s1++)
        for(s2ptr=s2;*s2ptr!='##代码##';s2ptr++) //warning
           if(*s1==*s2ptr)
               return s2ptr;

    return 0;
}

回答by Lundin

Doesn't const char *s means that "s is a pointer which is pointing towards a constant char"

不是 const char *s 意味着“s 是一个指向常量字符的指针”

Indeed it does. You get the warning because you are trying to convert this into a pointer pointing to a (non-constant) char. There is a rule in C saying that it is always ok to convert from pointer-to-type into pointer-to-const-type, but not the other way around.

确实如此。您收到警告是因为您试图将其转换为指向(非常量)字符的指针。C 中有一条规则说从指针类型转换为常量类型指针总是可以的,反过来不行

It doesn't matter if your code tries to change the values or not. Just by using char*you tell the compiler that you want a pointer which is allowed to change values.

您的代码是否尝试更改这些值并不重要。只需使用char*您告诉编译器您想要一个允许更改值的指针。

Most of the C standard library functions do not always make sense when it comes to "const correctness". There is for example no way to cleanly implement strchr. You will have to return (char*)sand cast away the const, which is very bad programming practice. This is the fault of the person who specified the strchrfunction: it is flawed by design.

大多数 C 标准库函数在涉及“常量正确性”时并不总是有意义。例如,没有办法干净地实现strchr. 您将不得不返回(char*)s并丢弃const,这是非常糟糕的编程实践。这是指定strchr函数的人的错:它在设计上有缺陷。

回答by Liju Thomas

For first warning: return discards 'const' qualifiers from pointer target type

对于第一个警告:返回丢弃来自指针目标类型的“const”限定符

C does not have an implicit conversion from const-qualifiedpointer types to non-const-qualified ones, so to overcome the warning you need to add it explicitly.

C 没有从const-qualified指针类型到非 const 限定类型的隐式转换,因此要克服警告,您需要显式添加它。

Replace return s;with return (char *)s;

替换return s;return (char *)s;

For second warning: assignment discards 'const' qualifiers from pointer target type

对于第二个警告:赋值会丢弃指针目标类型中的“const”限定符

  • 's2ptr'is of type char *and 's2'is of type const char *
  • You are not allowed to assign a const char*value to a char *pointer.
  • 's2ptr'是类型char *'s2'是类型const char *
  • 你是不是允许在指定const char*值的char *指针。

And regarding how to fix this warning... It depends on what you are trying to do. Either you can make char *s2ptras const char * s2ptror remove the constfrom s2.

关于如何解决此警告...这取决于您要尝试做什么。您可以制作char *s2ptrasconst char * s2ptr或删除constfrom s2

So if you wish to convert char *s2ptrto const char *s2ptr, do remember to explicitly cast s2ptrto (char *)s2ptrwhile returning it in the my_strpbrk()function.

所以,如果你想转换char *s2ptrconst char *s2ptr,千万记得要显式转换s2ptr(char *)s2ptr,而在它返回my_strpbrk()函数。

回答by Rishikesh Raje

For the first case, your return type is a charYou are trying to pass a const charas the return.

对于第一种情况,您的返回类型是 achar您正在尝试传递 aconst char作为返回。

For the second case, the problem is in s2ptr=s2. s2ptris a charand s2is a const char.

对于第二种情况,问题出在s2ptr=s2. s2ptr是一个char并且s2是一个const char