1、使用数组作为函数的参数
数组作为函数实参时,只传递数组的地址(首地址),并不传递整个数组的空间。当用数组名作为实参调用函数时,数组首地址指针就被传递到函数中。
2、一维数组作为函数参数
打印数组某一段区间的元素
#include <iostream>
using namespace std;
void show(const int*, const int*); //定义为const类型是为了保护数组中的数据
int main()
{
int valueArray[] = {90, 56, 67, 89, 100};
show(valueArray, valueArray + 1);
return 0;
}
void show(const int* begin, const int* end)
{
for(const int* ptr = begin; ptr <= end; ptr++)
{
cout << *ptr << "\t";
}
}
3、使用二维数组做参数
double_array.h 头文件书写:
#ifndef DOUBLE_ARRAY_H_INCLUDED
#define DOUBLE_ARRAY_H_INCLUDED
// 二维数组的传参
#include <iostream>
using namespace std;
void show(double (*)[5], int);
void show(double (*arrs)[5], int len)
{
for(int i = 0; i < len; i++)
{
for(int j = 0; j < 5; j++)
{
cout << arrs[i][j] << '\t';
}
cout << endl;
}
}
#endif // DOUBLE_ARRAY_H_INCLUDED
main文件代码书写:
#include <iostream>
#include "double_array.h";
using namespace std;
int main()
{
double powers[3][5]={
{45.5, 55.6, 88.9, 66.6, 78},
{98.2, 69.1, 33.7, 49.3, 58},
{78.2, 58.5, 12.8, 37.8, 43}
};
show(powers, 3);
}