简体   繁体   English

函数原型会在C中转换您的实际参数吗?

[英]Does a function prototype convert your actual parameters in C?

If you read the linux man page of any function and the prototype uses a keyword like static or restrict for any formal parameter, does C compiler auto-convert your var if the type still matches? 如果你读过任何功能的Linux手册页和原型使用关键字像staticrestrict任何形式参数,不C编译器自动转换您的变种,如果该类型仍然匹配?

For example: 例如:

Function prototype: int function_name(int* restrict param1, static int param2); 函数原型: int function_name(int* restrict param1, static int param2);

Program: 程序:

int *my_var1;
int my_var2;

//initialization
(..)

function_name(my_var1, my_var2);

(..)

Does function_name() convert or treat my variables like they were declared with restrict and static in each case due the type is still the same? 请问function_name()转换或者把我的变量,如他们与申报restrictstatic在每种情况下,由于该类型仍然是一样的吗?

A single int argument cannot be static , that has no meaning. 一个int参数不能为static ,没有任何意义。 Please provide actual real examples, to make sure we're talking about the same things. 请提供实际的实际示例,以确保我们在谈论同样的事情。

When it comes to restrict , it's used to express that the pointer qualified by it is the only pointer pointing at that particular object (more or less, I'm simplifying). 当涉及restrict ,它用来表示受其restrict的指针是指向该特定对象的唯一指针(或多或少,我正在简化)。 So it doesn't make sense to talk about "converting" a pointer to being restrict ed, whether or not the qualifier really applies depends on how the pointer is used. 因此,谈论将指针“转换”为restrict是没有意义的,限定符是否真正适用取决于指针的使用方式。

In general arguments will be converted to match what the function expects, when possible. 通常,在可能的情况下,将转换参数以匹配函数期望的参数。

First of all, int function_name(int* restrict param1, static int param2); 首先, int function_name(int* restrict param1, static int param2); is invalid. 是无效的。 Compiling under gcc 8.3 you will get following errors: 在gcc 8.3下编译时,您会收到以下错误:

<source>:1:52: error: storage class specified for parameter 'param2'
 int function_name(int* restrict param1, static int param2);

From cpprefrence implicit conversions : cpprefrence隐式转换

Conversion as if by assignment 转换就像是通过分配

... ...

In a function-call expression, to a function that has a prototype, the value of each argument expression is converted to the type of the unqualified declared types of the corresponding parameter 在函数调用表达式中,对于具有原型的函数,每个参数表达式的值都将转换为相应参数的非限定声明类型的类型

... ...

A pointer to an unqualified type may be implicitly converted to the pointer to qualified version of that type (in other words, const, volatile, and restrict qualifiers can be added. The original pointer and the result compare equal. 指向非限定类型的指针可以隐式转换为指向该类型的限定版本的指针(换句话说,可以添加const,volatile和strict限定符。原始指针和结果比较相等)。

You can convert a int* type: 您可以转换一个int*类型:

int *p;

into volatile const int * restrict volatile const : 进入volatile const int * restrict volatile const

volatile const int * restrict volatile const other_p = p;

without any errors. 没有任何错误。

Does function_name() convert or treat my variables like they were declared with restrict and static in each case due the type is still the same? 由于类型仍然相同,function_name()是否会像在变量和约束中声明的那样对我的变量进行转换或处理?

Yes. 是。 The variables are "implicitly converted" to the types in the function parameter list. 变量被“隐式转换”为功能参数列表中的类型。 They will be treated like declared inside the function. 它们将被视为在函数内部声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM