C/C++二维数组的传参方法总结 您所在的位置:网站首页 c二维数组作为函数参数的方法 C/C++二维数组的传参方法总结

C/C++二维数组的传参方法总结

2023-12-01 01:21| 来源: 网络整理| 查看: 265

C/C++二维数组的传参方法总结

转自:http://blog.csdn.net/gqb_driver/article/details/8886687

C/C++语言将二维数组作为参数传递容易使人迷惑且易出错,本文将常用的几种方法列出,以方便用时查阅。三+一种方法总结如下(GCC验证成功):

方法一:形参为二维数组并给定第二维长度  此方法是最简单最直观的方法,形参与实参一样,容易理解。

举例:

[cpp]  view plain copy #include       void subfun(int n, char subargs[][5])   {       int i;       for (i = 0; i "abc", "cde", "ghi"};       subfun(3, args);   }  

方法三:形参为指针的指针

此方法实参必须为指针,而不能为数组名

举例

[cpp]  view plain copy #include       void subfun(int n, char **subargs)   {       int i;        for (i = 0; i "abc", "def", "ghi"};//equals with char *args[3] = {"abc", "def", "ghi"};       subfun(3, args);   }  

------------------------------------------------------------------------------------------------------------------------

方法四:形参为指针+数组的行+数组的列

#include #include void test(double *p,const int m,const int n) { for(int i=0;i{1,2},{3,4}}; double **a; a = new double*[2]; for(int i =0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有