C++数组(指针)作为函数参数
1.当⽤数组名作为函数参数时,函数的实参和形参都应为数组名(或者指针);
Example1:有⼀个⼀维数组score,内放10个学⽣成绩,求平均成绩。
#include<iostream>
using namespace std;
float average(float array[],int num) {
int i;
float aver, sum = array[0];
for (i = 1; i< num; i++)
sum = sum + array[i];
aver = sum / num;
return aver;
}
int main()
{
float aver;
int number;
cout << "please input number:";
cin >> number;
float *score = new float[number];        //动态创建⼀维数组score
if (score == NULL)                        //检查内存空间是否分配完成
{
cout << "could not " << endl;
exit(1);
}
cout << "please input secores:" << endl;
for (int i = 0; i < number; i++)
cin >> score[i];
cout << endl;
aver = average(score,number);//数组名作为函数参数
cout << "the average score is " << aver << endl;
system("pause");
return0;
}
说明:
(1)⽤数组名称作函数参数,应该在主调函数和被调函数分别定义数组,本例中array是形参数组名,score是实参数组名,分别在其所在的函数中定义,不能只在⼀⽅定义;
(2)实参数组与形参数组类型应该保持⼀致(这⾥都为float型);
(3)形参数组可以不指定⼤⼩,此时可以在定义数组时,在数组名后⾯跟⼀个空的⽅括号;当需要在被调⽤函数中处理数组元素时,可以另设⼀个参数,传递需要处理的数组元素的个数;
如下所⽰,根据不同的数组长度来处理不同的数据Demo
#include<iostream>
using namespace std;
float average(float array[], int n) {
int i;
float aver, sum = array[0];
for (i = 1; i<n; i++)sum = sum + array[i];
aver = sum / n;
return aver;
}
void main() {
float score_1[5] = { 98.5,97,91.5,60,55 };
float score_2[10] = { 67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5 };
cout << "the average of class A is :" << average(score_1, 5) << endl;
cout << "the average of class B is :" << average(score_2, 10) << endl;
system("pause");
return;
}
sizeof 指针(4)⽤数组名作为函数实参的时,不是把数组元素的值传递给形参,⽽是把实参数组的起始地址传递给形参数组,这样两个数组就共占同⼀段内存单元。
#include<iostream>
#include<vector>
using namespace std;
char s[] = "1234567890";
int a[10] = { 0,1 };
int b[10] = { 0,1 };
void Display(char *c);
void PutArray1(int *p, int length);
void PutArray2(int p[], int length);
void PutArray3(int p[10]);
void PutArray4(int(&p)[10]);           
void PutArray5(vector<int> vec);          //向量作为函数参数
void main()
{
char q;
vector<int> vector(10,1);
cout << "这个是在主函数中对数组长度的测试:" << sizeof(s) << endl;
Display(s);
cout << "-------------------------------" << endl;
PutArray1(a, 10);
PutArray2(a, 10);
PutArray3(a);
PutArray4(b);
PutArray5(vector);
system("pause");
return;
}
void Display(char *c)
{
cout << "这个是在⼦函数中对数组长度的测试:" << sizeof(c) << endl;
}
void PutArray1(int *p, int len)
{
int length1 = sizeof(p);
cout << "第⼀种⽅法的输出:" << endl;
cout << "第⼀种⽅法数组的长度为:" << length1 << endl;
for (int i = 0;i < length1;i++)
{
cout << p[i] << "";
}
}
cout << endl;
}
void PutArray2(int p[], int len)
{
int length = sizeof(p);
cout << "第⼆种⽅法的输出:" << endl;
cout << "第⼆种⽅法数组的长度为:" << length << endl;
for (int i = 0;i < length;i++)
{
cout << p[i] << "";
}
cout << endl;
}
void PutArray3(int p[10])
{
int length = sizeof(p);
cout << "第三种⽅法的输出:" << endl;
cout << "第三种⽅法数组的长度为:" << length << endl;
for (int i = 0;i < length;i++)
{
cout << p[i] << "";
}
cout << endl;
}
void PutArray4(int(&p)[10])
{
int length = sizeof(p);
cout << "第四种⽅法的输出:" << endl;
cout << "第四种⽅法数组的长度为:" << length << endl;
for (int i = 0;i < length;i++)
{
cout << p[i] << "";
}
cout << endl;
}
void PutArray5(vector<int> vec)
{
vector<int>::iterator begin_iter = vec.begin();
vector<int>::iterator end_iter = d();
int size = vec.size();
cout << "第五种⽅法的输出:" << endl;
cout << "第五种⽅法数组的长度为:" << size << endl;
cout << "下⾯这种⽅法是采⽤向量遍历的⽅法遍历数组:" << endl;
for (vector<int>::iterator iter = begin_iter;iter != end_iter;iter++)
{
cout << *iter << "";
}
cout << endl;
cout << "下⾯这种⽅法是采⽤普通遍历数组的⽅法遍历数组:" << endl;
for (int i = 0;i < size;i++)
{
cout << vec[i] << "";
}
cout << endl;
}
result:
总结:
(1)在这⾥,int *arr和int arr[]的含义相同,编译器⾃动将 int arr[]替换为int *arr,所以这也解释了上⾯在主函数和⼦函数中利⽤数组名求数组长度会得到不同结果的原因。这种情况只有在数组作为函数参数进⾏传递时才会发⽣;
(2)在⼦函数中⽆法直接获得作为参数的数组的长度,因此数组的长度必须在函数中作为参数进⾏传递;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。