posts - 137,comments - 0,views - 40399

数组作为函数的参数传递,不是单纯的值传递,传递的是数组本身(访问的是同一个地址,相同的值)。

版本1:指明参数:

复制代码
#include <iostream>
#include <windows.h>
using namespace std;

void part1(int put[3][4]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            cout << put[i][j] << " ";
        }
        cout << endl;
    }
}
int main() {
    int x = 0;
    int y = 0;
    int stat[3][4] = { 0 };
    for (x = 0; x < 3; x++) {
        for (y = 0; y < 4; y++) {
            stat[x][y] = 4 * x + y + 1;
        }    
    }

    part1(stat);

    system("pause");
    return 0;
}
复制代码

版本2:省略一个高维参数:

复制代码
#include <iostream>
#include <windows.h>
using namespace std;

void part1(int put[][4],int lines) {
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < 4; j++) {
            cout << put[i][j] << " ";
        }
        cout << endl;
    }
}
int main() {
    int x = 0;
    int y = 0;
    int stat2[3][4] = { 0 };
    for (x = 0; x < 3; x++) {
        for (y = 0; y < 4; y++) {
            stat2[x][y] = 4 * x + y + 1;
        }
    }

    part1(stat2,3);

    system("pause");
    return 0;
}
复制代码

posted on   wshidaboss  阅读(125)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示