我只是想知道我是否可以做类似的事情...
typedef struct Result{
int low, high, sum;
} Result;
Result test(){
return {.low = 0, .high = 100, .sum = 150};
}
我知道那不是正确的方式,但我能这样做吗?或者我需要创建一个本地变量来接收值,然后再返回它吗?
我只是想知道我是否可以做类似的事情...
typedef struct Result{
int low, high, sum;
} Result;
Result test(){
return {.low = 0, .high = 100, .sum = 150};
}
我知道那不是正确的方式,但我能这样做吗?或者我需要创建一个本地变量来接收值,然后再返回它吗?
Result test(void)
{
return (Result) {.low = 0, .high = 100, .sum = 150};
}
(){}
是复合字面量运算符,复合字面量是 c99 引入的一个特性。
struct Result
{
int low;
int high;
int sum;
};
then to create an instance of the struct
struct Result myResult;
Regarding your question...
prototype for the test function
void test( struct Result *myResult );
invoke the function by:
test( &myResult );
the test function:
void test( struct Result *argResult )
{
argResult->low = 0;
argResult->high = 100;
argResult->sum = 150;
}
struct
通常会在两个寄存器中返回。 - Basile Starynkevitchreturn (Result){0, 100, 150};
。 - tleb