介绍
本篇文章给大家分享的是有关c++中函数怎么返回多个值,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
<强> 1。遇到了一个棘手的问题:(C/c++函数如何返回多个值? ? ?)强>
我将算法都封装成不同的函数,在matlab中调用该函数很简单,如下:
% matlab 函数示例 function [outputArg1, outputArg2],=, test_func (inputArg1 inputArg2) %, test_func 此处显示有关此函数的摘要%,,,此处显示详细说明 outputArg1 =, inputArg1; outputArg2 =, inputArg2;结束
在matlab中,想输出多个值很容易。
但是,你要想<标记>在C语言函数中返回(输出)多个值,没有这么容易。马克>
<强> 2。怎么办?(代码示例)强>
方法有3种,我只说最常用也最有效的1种。
<标记>在函数调用时,传递带有地址的参数,并使用指针更改其值;
这样,修改后的值就会变成原始参数。>标记
直接上代码:
1)主函数//main.cpp
//main.cpp # include & lt; stdio.h> # include “mycal.h" int ,主要(){ ,, ,, ,,int a =, 2; int b =, 1; int c; int c1; myadd_func (a、b和c,和c1); printf (“c=, % d \ n" (c); printf (“c1=, % d \ n",, c1); return 0;}
2)子函数//mycal.cpp
//mycal.cpp # includeint myadd_func (int int b, int * c , int * c1) { ,, ,, ,,* c =, a +, b; * c1=, a +, b + 100; return 0;}
3)子函数对应的头文件
//mycal.hint myadd_func (int ,, int b, int * c, int * c1);
<强>运行结果:强>
以上就是c++中函数怎么返回多个值,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。