返回两值源码:
#include <stdio.h>
typedef struct
{
int a;
int b;
} Stu;
/*结构体返回两值或多值*/
Stu getStu(int x, int y)//以结构体定义函数的类型
{
Stu result;
result.a = x + 1 + x*x;
result.b = y + 2017;
return result;
}
int main()
{
int a = 2, b = 3;
Stu test = getStu(a, b);//调用函数返回值
printf("%d\n %d\n", test.a, test.b);
return 0;
}
执行结果:
7
2020
[Inferior 1 (process 13844) exited normally]
类似的可以返回四个值:
#include <stdio.h>
typedef struct
{
int a;
int b;
double c;
double d;
} Stu;
/*结构体返回两值或多值*/
Stu getStu(int x, int y)//以结构体定义函数的类型
{
Stu result;
result.a = x + 1 + x*x;
result.b = y + 2017;
result.c = y + 10.14;
result.d = y + 517;
return result;
}
int main()
{
int a = 2, b = 3;
Stu test = getStu(a, b);//调用函数返回值
printf("%lf\n %lf\n", test.c, test.d);
printf("求和:%lf\n", test.c + test.d);
return 0;
}
执行结果;
13.140000
520.000000
求和:533.140000
[Thread 36236.0x6dac exited with code 0]
[Inferior 1 (process 36236) exited normally]