数组与函数

当你想在函数中使用数组的时候,第一时间的想法,唉直接将数组作为参量嘛~,简单。

void fillArray(int arr[size]);

当运行的时候便傻眼了,hhhhh。 

其实函数中的参数列表中是没有数组变量的,当我们需要使用函数调用数组的话,被调用的对象只能是数组地址,即以下两种形式。

void fillArray(int arr[],int size);
void fillArray(int* arr,int size);

特别注意:int* arr 与 int arr[] 只在函数体中意义才相同,即均表示数组的地址。

实例如下所示:

#include <iostream>
const int ArSize = 8;
using namespace std;
// 创建arr[]的过程便相当于指针
int int_arr(int arr[], int n);
int main()
{
    // 计算总数
    int total;
    int cookies[ArSize] = { 1,2,4,8,16,32,64,128 };
    cout << "cookies = " << cookies << endl;
    cout << "sizeof(cookies) = " << sizeof(cookies) << endl;
    cout << "我想知道全家吃了多少饼干:\n";

    total = int_arr(cookies, ArSize);
    cout << total << endl;

    cout << "前三个人所吃的饼干数:" << int_arr(cookies, 3) << endl;
    cout << "后四个人所吃的饼干数:" << int_arr(cookies + 4, 4) << endl;

    //std::cout << "Hello World!\n";
}
int int_arr(int arr[], int n)
{
    int sum = 0;
    // 这里返回的是指针的地址
    cout << "arr = " << arr << endl;
    cout << "sizeof(arr) = " << sizeof(arr) << endl;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    return sum;
}

上述实例通过指针地址来访问数组cookies中的元素。

那么此时有同学会问了,将地址直接传递给函数,不担心函数内部将值修改嘛?答案当然是担心,因此Const 应运而生。故当我们设计函数数组的时候,对于仅起显示作用的函数,将其涉及到数组的参量设置为const 。实例如下所示,该代码,通过fii_arr()函数填补数组,在show_arr函数中使用const 说明该函数只负责显示数组,而不能修改数组。

#include <iostream>
const int ArSize = 8;
const int Max = 5;
using namespace std;
int fill_arr(double ar[],int n);
void show_arr(const double ar[], int n);
void re_value(double r, double ar[], int n);
int main()
{
    // 创建数组
    double properties[Max];

    int size = fill_arr(properties, Max);
    // 有数字才进行显示嘛
    if (size > 0)
    {
        show_arr(properties, size);
        cout << "输入影响因子:\n";
        double factor;
        // 若输入错误,则一直叫他输入直到输入正确为止
        while (!(cin >> factor))
        {
            cin.clear();
            cout << "请输入数字!\n";
            while (cin.get() != '\n')
                continue;
            cout << "请重新输入。\n";
        }
        re_value(factor, properties, size);

        show_arr(properties, size);
    }
}
void show_arr(const double ar[], int n)
{
    // 展示数组
    for (int i = 0; i < n; i++)
    {
        cout << "value#" << i << ":" << ar[i] << endl;
    }
}
void re_value(double r, double ar[], int n)
{
    // 更改数组的值
    for (int i = 0; i < n; i++)
    {
        ar[i] *= r;
    }
}
int fill_arr(double ar[], int n)
{
    double value;
    int i;
    for ( i = 0; i < n; i++)
    {
        cout << "输入value" << i << ":\n";
        cin >> value;
        if (!cin)
        {
            //若输入非数组
            //首先清楚错误输入缓存
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "程序终止!\n";
            break;
        }
        if (value <= 0)
        {
            cout << "输入了小于0的数值,程序终止!\n";
            break;
        }
        ar[i] = value;
    }
    return i;
}

以下插播const 的一般使用方法:

int sloth = 32;
const int* pt = &sloth; 
/***上面的const 意味着你不能通过pt来修改sloth中的值***/
*pt = 30 ; // not allowed 
/***但是你可以更改pt的地址即***/
pt = & beatuy ;




int sloth = 32;
int* const pt = &sloth ;
/***上述代码意味着pt只能指向sloth ***/
/***但可以允许pt 修改sloth中的值***/
*pt = 30 ; // allowed 
pt = & beatuy ;// not allowed

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值