[英]function parameter is structure, assigning its members a value at call time
Take for instance the following example of calling a function, and assigning a value to one of its parameters at call time: 以下面的示例为例,该函数调用一个函数,并在调用时将值分配给其参数之一:
int MyFunction(int flag, float value);
MyFunction(myFlag=666, 3.14);
It's legal, though perhaps dangerous, to assign values to variables in a function call's parameter list. 将值分配给函数调用的参数列表中的变量是合法的,尽管也许很危险。
How can I do something similar when a function's parameter may be a structure? 当函数的参数可能是结构时,我该如何做类似的事情?
typedef struct
{
int i, j;
float k;
} my_struct;
int MyFunction(my_struct values);
myValues.i = 666;
myValues.k = 3.14;
MyFunction(myValues);
// how about this instead:
MyFunction({myValues.j=777}); // or something similar?
您可以为变量分配复合文字:
MyFunction(myValues = (my_struct) {myValues.i, .j=777, myValues.k});
The comma operator can be used in function arguments. 逗号运算符可在函数参数中使用。 You need some extra parentheses so it doesn't look like an argument-separating comma: 您需要一些额外的括号,以免看起来像分隔参数的逗号:
MyFunction((myValues.j=777, myValues));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.