函数返回多个参数给主函数|
函数之间结构体如何传递参数
笔者,最近因为学校项目需要,又捡起了vs2017来写算法 |
后面会更新矩阵使用方法,和自己写的一些矩阵功能,有兴趣的关注一波~ |

不多BB,直接上代码(共两种方法) |
main.cpp
#include <iostream>
using namespace std;
#include "jiegouti.h"
int main() {
cout << "*********************************************************************" << endl;
cout << "vs2017 从一个函数返回多个参数给主函数|函数之间结构体如何传递参数" << endl;
cout << endl << endl;
/*第一种方法*/
listptr a = NULL;
a = (listptr)malloc(sizeof(list));
*a = (re)malloc(sizeof(Result));
a= first(1, 2);
printf("第一种方法:------------------------------\n");
cout << *((*a)->s_arr) << endl;
printf("position:%d \ns_arr:%d,%d,%d", (*a)->position,(*a)->s_arr[0], (*a)->s_arr[1], (*a)->s_arr[2]);
cout << endl << endl;
/*第二种方法*/
PARAMSAVE set;
set = second(1, 2);
printf("\n第二种方法:------------------------------\n");
printf("position:-------------------------\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 10; j++)
printf("%f ", set.position[i][j]);
cout << endl;
}
printf("s_arr:-------------------------\n");
for (int i = 0; i < 10; i++)
printf("%f :", set.s_arr[i]);
system("pause");
}
第二个文件
jiegouti.h
#pragma once
#ifndef JIEGOUTI_H
#define JIEGOUTI_H
/*第一种方法*/
typedef struct
{
int position[2][3];;
int s_arr[3];
}Result,*re;
typedef re list, *listptr;
extern listptr first(int x, int y);
/*第二种方法*/
typedef struct ParamSave {
double position[3][10 + 1];
double s_arr[10];
}PARAMSAVE;
extern PARAMSAVE second(int startPoint, int middlePoint);
#endif JIEGOUTI_H
头文件
jiegouti.cpp
#include <iostream>
using namespace std;
#include "jiegouti.h"
/*第一种方法*/
listptr first(int x, int y) {
listptr ret = NULL;
ret = (listptr)malloc(sizeof(list));
*ret = (re)malloc(sizeof(Result));
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
(*ret)->position[i][j] = x + y;
}
}
(*ret)->position[0][0] = x+y;
(*ret)->s_arr[0] = 1;
(*ret)->s_arr[1] = 1;
(*ret)->s_arr[2] = 1;
return ret;
}
/*第二种方法*/
PARAMSAVE haha;
PARAMSAVE second(int x, int y) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 10; j++)
haha.position[i][j] = x + y;
for (int i = 0; i < 10; i++)
haha.s_arr[i] = 1;
return haha;
}

资源领取:
1.在公众号后台领取,里面有我分享的学习资源,文件命名:vs2017结构体传参
2.或者去找我上传到压缩包《vs2017 c语言、c++从一个函数返回多个参数给主函数|c++函数之间结构体如何传递参数》
后续更新ST语言文件读取cvs |
codesys工程ST语言学习笔记(二)写文件操作与添加库方法
codesys工程ST语言学习笔记(三)读文件操作txt,访问hello world 文件

【关注微信公众号一起来交流】 |
