传递参数1会从指针目标类型中丢弃限定词 [英] Passing Argument 1 discards qualifiers from pointer target type
问题描述
我的主要功能如下:
int main(int argc, char const *argv[])
{
huffenc(argv[1]);
return 0;
}
编译器返回警告:
huffenc.c:76: warning: passing argument 1 of ‘huffenc’ discards qualifiers from pointer target type
huffenc
作为参考,huffenc
接受一个char*
输入,然后通过./huffenc senselessness
For reference, huffenc
takes a char*
input, and the function is executed, with the sample input "senselessness" via ./huffenc senselessness
此警告意味着什么?
推荐答案
这意味着您正在将const
参数传递给带有非const
参数的函数,由于明显的原因,该函数可能很糟糕.
It means that you're passing a const
argument to a function which takes a non-const
argument, which is potentially bad for obvious reasons.
huffenc
可能不需要非const
参数,因此应使用const char*
.但是,您对main
的定义是非标准的.
huffenc
probably doesn't need a non-const
argument, so it should take a const char*
. However, your definition of main
is non-standard.
C99标准第5.1.2.2.1节(程序启动)规定:
The C99 standard Section 5.1.2.2.1 (Program startup) states:
在程序启动时调用的函数称为main.实现声明为否 此功能的原型.它应使用返回类型int定义,且不包含 参数:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
或带有两个参数(此处称为argc和argv,尽管任何名称都可以是 使用,因为它们对于声明它们的函数是局部的):
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):
int main(int argc, char *argv[]) { /* ... */ }
或等效形式; 9)或以其他实现方式定义的方式.
or equivalent;9) or in some other implementation-defined manner.
接着说...
...参数argc和argv以及argv数组指向的字符串 可由程序修改,并在程序之间保留其最后存储的值 启动和程序终止.
...The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
这篇关于传递参数1会从指针目标类型中丢弃限定词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!