return不能直接返回多個值碉纳,如果你想通過函數(shù)內(nèi)部返回多個值的話,一般有三種方法:
第一種:返回結構體
#include <stdio.h>
//定義一個結構體
typedef struct _a
{
int a;
int b;
}A,*PA;
//函數(shù)返回結構體變量,它里面就可以包含多個值
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;
}
第二種:以引用方式傳遞函數(shù)參數(shù)
#include <stdio.h>
//要以引用方式傳遞參數(shù)件已,否則,在函數(shù)內(nèi)部改變形式參數(shù)的值,
//函數(shù)返回之后啄巧,參數(shù)值依然不變
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;
}
第三種:以類型指針方式傳遞函數(shù)參數(shù)
#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;
}