使用二维数组作为函数参数

法一

void show(double (*)[5], int); //函数声明
void show(double (*arr)[5], int len){ }; //函数实现

#include <iostream>

using namespace std;

void show(double (*)[5], int);

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);
    return 0;
}

void show(double (*arr)[5], int len)
{
    for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            //等价于cout << arr[i][j] << " ";
            cout << *(*(arr + i)+j) << " ";
        }
        cout << endl;
    }
}

法二

void show(double [][5], int); //函数声明
void show(double arr[][5], int len) { }; //函数实现

#include <iostream>

using namespace std;

void show(double [][5], int);

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);
    return 0;
}

void show(double arr[][5], int len)
{
    for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            //等价于cout << arr[i][j] << " ";
            cout << *(*(arr + i)+j) << " ";
        }
        cout << endl;
    }
}

如果是string类呢?

把double类型改为string即可

#include <iostream>

using namespace std;

void show(string (*)[5], int);

int main()
{
    string powers[2][5] = {
    {"东邪黄药师", "西毒欧阳锋", "南帝段智兴", "北丐洪七公", "中神通王重阳"},
    {"东邪黄药师", "西狂杨过", "南僧一灯", "北侠郭靖", "中顽童周伯通"},
    };
    show(powers, 2);
    return 0;
}

void show(string (*arr)[5], int len)
{
    for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            //等价于cout << arr[i][j] << " ";
            cout << *(*(arr + i)+j) << " ";
        }
        cout << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值