利用结构体让函数返回多个值

c语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdio.h"
typedef struct test //建立结构体
{
int a;
int b;
int c;
}T;

T testt(int aa,int bb,int cc) //建立函数
{
T aaa;
aaa.a=aa*bb;
aaa.b=aa*cc;
aaa.c=bb*cc;
return aaa; //返回结构
}

int main()
{
T b=testt(4,5,6);
printf("%d %d %d \n",b.a,b.b,b.c);
T *f=&b;
printf("%d %d %d",f->a,f->b,f->c);
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "iostream"
typedef struct sss //建立结构体
{
int a;
int b;
int c;
}S;

S s(double a,double b,double c) 建立函数
{
S d;
d.a=a*a;
d.b=b*b;
d.c=c*c;
return d;//返回结构体
}

int main()
{
S z=s(3,4,5);
std::cout<<z.a
<<std::endl
<<z.b
<<std::endl
<<z.c
<<std::endl;
}