【问题标题】:passing an 2d array pointer to a function in c++ [duplicate]将二维数组指针传递给c ++中的函数[重复]
【发布时间】:2018-12-08 05:05:57
【问题描述】:

我知道如何通过以下代码将一维数组指针传递给函数

void fiddleWithArray(int*);   

int main(){

int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};
cout << "List at 0 before being passed is... " << list[0][0] << endl;    
cout << "List at 1 before being passed is... " << list[1][0] << endl;    
fiddleWithArray(list);
cout << "List at 0 after being passed is... " << list[0][0] << endl;    
cout << "List at 1 after being passed is... " << list[1][0] << endl;    

}

void fiddleWithArray(int* input){
input[0] = 45;
input[1] = 18;
}

但是,当我尝试对 2D 数组执行类似操作时(如下所示),出现错误。

void fiddleWithArray (int** input);

int main ()
{
int list [10][2]={{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
int ** pointer;
pointer=&list;
cout<< "List at 0 before being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 before being passed is ... "<< list[1][0]<< endl;
fiddleWithArray(pointer);
cout<< "List at 0 after being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 after being passed is ... "<< list[1][0]<< endl;
}

void fiddleWithArray(int** input)
{   
cout << input [6][1]<< endl;
}

编译器给出错误提示“错误:无法在赋值中将‘int (*)[10][2]’转换为‘int**’ 指针=&list;" 我也对将二维数组指针传递给函数的替代方法持开放态度。

【问题讨论】:

    标签: c++ arrays function pointers


    【解决方案1】:

    保持你的数据结构,如果你想将list 传递给fiddleWithArray 你可以将它声明为

    void fiddleWithArray (int input[][2]);
    

    然后,在主程序中,将其称为

    fiddleWithArray(list);
    

    您的程序中还有另一个问题:cout &lt;&lt; list[0] 不起作用。如果您想在第一个索引固定为 0 时打印数组的内容,您可以编写类似

    cout << list[0][0] << " " << list[0][1]
    

    如果您打算编写 second 索引固定为 0 或 1 的数组,那么,为了方便保存,您需要一个短循环,例如

    for (unsigned int i = 0; i < 10; i++)
       cout << list[i][0] << " ";
    cout << endl;
    

    最后,您可能希望使用 C++11 中引入的 std::array,而不是使用 int[][]

    【讨论】:

    • 非常感谢您的帮助。你的建议解决了我的问题。 :)
    • 好答案。建议将 void fiddleWithArray (int input[][2]); 更改为 void fiddleWithArray (int input[][2], size_t len); 或类似的东西,以便 fiddleWithArray 知道何时停止迭代。
    • @francesco 也是一个小小的澄清。 void fiddleWithArray 中的 [2] (int input[][2]);是 2 是因为它是一个二维数组还是因为列表数组中的第二个参数是 2?
    • @McMissile fiddleWithArray 声明中的[2] 指的是第二维的大小。事实上,在将多维数组声明为参数时,您必须指定除第一个维度之外的所有维度的大小。
    【解决方案2】:
    void fiddleWithArray(int input[10][2])
    {
        cout << input[6][1] << endl;
    }
    
    int main()
    {
        int list[10][2] = {{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
        int (*pointer)[10][2];
        pointer=&list;
        cout << "List at 0 before being passed is ... "<< list[0][0]<< endl;
        cout << "List at 1 before being passed is ... "<< list[1][0]<< endl;
        fiddleWithArray(*pointer);
        cout << "List at 0 after being passed is ... "<< list[0][0]<< endl;
        cout << "List at 1 after being passed is ... "<< list[1][0]<< endl;
    }
    

    使用std::array会更好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2014-05-09
      • 1970-01-01
      • 2011-07-16
      • 2013-01-18
      相关资源
      最近更新 更多