C语言 传递参数 1 丢弃来自指针目标类型的限定符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15398580/
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 05:42:11  来源:igfitidea点击:

Passing Argument 1 discards qualifiers from pointer target type

cmainargv

提问by Alex Nichols

My main function is as follows:

我的主要功能如下:

int main(int argc, char const *argv[])
{
    huffenc(argv[1]);
    return 0;
}

The compiler returns the warning:

编译器返回警告:

huffenc.c:76: warning: passing argument 1 of ‘huffenc' discards qualifiers from pointer target type

huffenc.c:76: warning: passing argument 1 of ‘huffenc' discards qualifiers from pointer target type

For reference, huffenctakes a char*input, and the function is executed, with the sample input "senselessness" via ./huffenc senselessness

作为参考,huffenc接受一个char*输入,然后执行函数,示例输入“无意义”通过./huffenc senselessness

What could this warning mean?

这个警告意味着什么?

回答by Ed S.

It means that you're passing a constargument to a function which takes a non-constargument, which is potentially bad for obvious reasons.

这意味着您将一个const参数传递给一个采用非const参数的函数,由于显而易见的原因,这可能是不好的。

huffencprobably doesn't need a non-constargument, so it should take a const char*. However, your definition of mainis non-standard.

huffenc可能不需要非const参数,所以它应该使用const char*. 但是,您的定义main是非标准的。

The C99 standard Section 5.1.2.2.1 (Program startup) states:

C99 标准第 5.1.2.2.1 节(程序启动)指出:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be de?ned with a return type of int and with no parameters:

程序启动时调用的函数名为 main。实现声明没有此函数的原型。它应定义为返回类型为 int 且不带参数:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

或带有两个参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于声明它们的函数而言是本地的):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-de?ned manner.

或等价物;9) 或以其他一些实现定义的方式。

And goes on to say...

并继续说...

...The parameters argc and argv and the strings pointed to by the argv array shall be modi?able by the program, and retain their last-stored values between program startup and program termination.

...参数 argc 和 argv 以及 argv 数组指向的字符串应可由程序修改,并在程序启动和程序终止之间保留它们最后存储的值。