简体   繁体   中英

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?

For example:

Function prototype: 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?

A single int argument cannot be static , that has no meaning. 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). 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.

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); is invalid. Compiling under gcc 8.3 you will get following errors:

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

From cpprefrence implicit conversions :

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.

You can convert a int* type:

int *p;

into 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?

Yes. The variables are "implicitly converted" to the types in the function parameter list. They will be treated like declared inside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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