zoukankan      html  css  js  c++  java
  • 二维数组的函数参数传递

    两种方式:

    1. 静态二维数组,如int a[10][20];

    2. 二维指针创建的动态数组,如int** p.

    第一种:

     1 int GetMaxElem(int (*ppArray)[20], int nRow, int nColumn)
     2 {
     3     ASSERT(ppArray != NULL);
     4     ASSERT(nRow > 0 && nColumn > 0);
     5 
     6     int nMax = ppArray[0][0];
     7 
     8     for (int i=0; i<nRow; i++)
     9     {
    10         for (int j=0; j<nColumn; j++)
    11         {
    12             if (ppArray[i][j] > nMax)
    13             {
    14                 nMax = ppArray[i][j];
    15             }
    16         }
    17     }
    18 
    19     return nMax;
    20 }

     调用时如下:

    1 int  (*p)[20= a;
    2 int nMax = GetMaxElem(p, 1020);

     另外,还可以使用以下方式:

    1 typedef int MATRIX[10][20];
    2 int GetMaxElem(MATRIX ppArray, int nRow, int nColumn)

    调用时使用:

    1 int nMax = GetMaxElem(p, 1020);
    2 int nMax = GetMaxElem(a, 1020);

    第二种:

     1 int GetMaxElem(int** ppArray, int nRow, int nColumn)
     2 {
     3     ASSERT(ppArray != NULL);
     4     ASSERT(nRow > 0 && nColumn > 0);
     5 
     6     int nMax = ppArray[0][0];
     7 
     8     for (int i=0; i<nRow; i++)
     9     {
    10         for (int j=0; j<nColumn; j++)
    11         {
    12             if (ppArray[i][j] > nMax)
    13             {
    14                 nMax = ppArray[i][j];
    15             }
    16         }
    17     }
    18 
    19     return nMax;
    20 }
    不过调用的时候就和第一种不同了。需要先将a的内容拷贝到一个动态数组中。
  • 相关阅读:
    DbUtils类基本使用
    【struts2】ActionContext与ServletActionContext
    Eclipse 菜单---Eclipse教程第04课
    Eclipse 窗口说明---Eclipse教程第03课
    Eclipse 修改字符集---Eclipse教程第02课
    Java 开发环境配置
    Eclipse 安装(Neon 版本2016年)---Eclipse教程第01课
    eclipse中link方式安装插件
    linux 源码安装mysql 5.5
    shell执行mysql命令
  • 原文地址:https://www.cnblogs.com/burellow/p/2043525.html
Copyright © 2011-2022 走看看