Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中函数前的const关键字_C - Fatal编程技术网

C中函数前的const关键字

C中函数前的const关键字,c,C,我想知道在“C”中的函数之前,const关键字的作用是什么 例如: 提前谢谢这没有任何意义。如果没有显式指定返回类型,那么当C允许隐式返回类型int时,似乎是一些旧代码有效。但无论如何,返回值不是左值,不能更改。因此常量限定符是多余的。如果打开警告,则将有两个: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] warning: 'const' type qualifier on return type has

我想知道在“C”中的函数之前,const关键字的作用是什么

例如:


提前谢谢

这没有任何意义。如果没有显式指定返回类型,那么当C允许隐式返回类型int时,似乎是一些旧代码有效。但无论如何,返回值不是左值,不能更改。因此常量限定符是多余的。

如果打开警告,则将有两个:

warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
这很容易让你得出这样的结论:

extern const func1(int x[], const struct dummy_s* dummy)
基本上与:

extern int func1(int x[], const struct dummy_s* dummy)

在标准C中,您的代码不会编译。不允许省略C函数的返回类型

在旧版本的C(称为C90)中,您可以省略返回类型,在这种情况下,返回类型将默认为int。在旧版本的C中,您的代码将等于:

extern const int func1(int x[], const struct dummy_s* dummy);

接下来的问题是,从函数返回常量int而不仅仅是int有意义吗?不,它不。。。因为返回的变量总是放在堆栈上的硬拷贝。它不是一个左值,而且函数是在运行时执行的,无论如何,没有理由要求该值为常量。

可能重复@Coconop:我想他指的是mods的第一个常量,sry。“真遗憾,我无法挽回这一切。”约翰尼莫普;C++!C.类型丢失了,所以这肯定是您在某处找到的,您可以使用确切的行吗?不要只打开警告,将您的C代码编译为标准C.gcc-std=c11-pedantic errors-Wall-Wextra。您现在将得到错误,而不仅仅是警告。@Lundin这完全取决于用户使用的标准。如果他们使用的是C89,那么它应该是完全有效的代码。“你不能假设自2011年以来所有的程序都已经完成了:你总是可以将一个非常量变量分配给一个常量变量——我想你的意思是从一个常量变量分配给一个常量变量。”davmac试图澄清一下。
extern const int func1(int x[], const struct dummy_s* dummy);