• C语言二维数组的传递几种方法


    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // 方法1:传递了二维数组的首地址,和二维数组的行数
    void
    print1_Array2D(int (*ptr)[4], int cnt) { for (int i = 0; i < cnt; i++) { for (int j = 0; j < 4; j++) { cout << ptr[i][j] << " "; } cout << endl; } }
    // 方法2:传递了二维数组的首地址,和二维数组的行数
    void print2_Array2D(int ptr[][4], int cnt) { for (int i = 0; i < cnt; i++) { for (int j = 0; j < 4; j++) { cout << ptr[i][j] << " "; } cout << endl; } }
    // 方法3:传递了二维数组第一个元素的地址,和而为数组的行数和列数
    void print3_Array2D(int *ptr, int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << *(ptr + i * col + j) << " "; } cout << endl; } } int main(int argc, char *argv[]) { int iArray2D[3][4] = { {12, 3, 5 ,12}, {23, 4, 8 ,10}, {10, 7, 3 ,18} }; print1_Array2D(iArray2D, 3); cout << endl; print2_Array2D(iArray2D, 3); cout << endl; print3_Array2D(&iArray2D[0][0], 3, 4); return 0; }
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // 方法4:将二维数组的每行地址传递给指针数组,及二维数组的行数和列数
    void print4_Array2D(int* ptr[], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << *(ptr[i] + j) << " "; } cout << endl; } }
    // 方法5:与方法4本质相同
    void print5_Array2D(int** ptr, int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << *(ptr[i] + j) << " "; } cout << endl; } } int main(int argc, char *argv[]) { int iArray2D[3][4] = { {12, 3, 5 ,12}, {23, 4, 8 ,10}, {10, 7, 3 ,18} }; int* ptr[3] = {iArray2D[0], iArray2D[1], iArray2D[2]}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { cout << *(ptr[i] + j) << " "; } cout << endl; } cout << endl; print4_Array2D(ptr, 3, 4); cout << endl; print5_Array2D(ptr, 3, 4); return 0; }
  • 相关阅读:
    GridView多表头固定+分组+总计
    ajaxpro.2.dll使用【转帖】
    表达式计算易错题
    uclibc下使用libcurl的段错误(缺少hosts文件)
    《java.util.concurrent 包源码阅读》01 源码包的结构
    Linux学习笔记【2】Install Software under RedHat enterprise 5.4
    Windows语言包的那些事
    Let outlook work background when it is minimal
    DB2 Error Message
    db2 系统表信息
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3062529.html
Copyright © 2020-2023  润新知