return不能直接返回多个值,如果你想通过函数内部返回多个值的话,一般有三种方法:
第一种:返回结构体
#include <stdio.h>
//定义一个结构体
typedef struct _a
{
int a;
int b;
}A,*PA;
//函数返回结构体变量,它里面就可以包含多个值
PA func()
{
PA a = new A();
a->a = 2;
a->b = 3;
return a;
}
int main()
{
PA test = func();
printf("%d %d\n", test->a, test->b);
delete test;
return 0;
}
第二种:以引用方式传递函数参数
#include <stdio.h>
//要以引用方式传递参数,否则,在函数内部改变形式参数的值,
//函数返回之后,参数值依然不变
void func(int& a, int& b)
{
a = 2;
b = 3;
}
int main()
{
int a = 0;
int b = 0;
func(a, b);
printf("%d %d\n", a, b);
return 0;
}
第三种:以类型指针方式传递函数参数
#include <stdio.h>
void func(int* a, int* b)
{
*a = 2;
*b = 3;
}
int main()
{
int a = 0;
int b = 0;
func(&a, &b);
printf("%d %d\n", a, b);
return 0;
}