C++数组(指针)作为函数参数

本文的求学内容参考:http://blog.csdn.net/wwdlk/article/details/6322843

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 allocate.Bye..." << 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");
    return 0;
}

 说明:

(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;
}

(4)用数组名当函数实参的时,不是拿数组元素的价值传递让形参,而是把的参数组的开场地址传递给形参反复组,这样少独数组就旅占用及同截内存单元

2.因此数组作为函数参数的集中情况(学习内容参考http://blog.csdn.net/zeusuperman1/article/details/8509287)

#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 = vec.end();
    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

 

总结:

(1)在这里,int *arrint arr[]义相同,编译器自动将 int
arr[]替换为int
*arr,所以这也说明了方在主函数和子函数中以数组名求数组长度会赢得不同结果的来由。这种情形只有在频繁组作为函数参数进行传递时才会生出;

(2)在子函数中无法直接拿走当参数的数组的尺寸,因此往往组的长短要在函数中作为参数进行传递;