gpt4 book ai didi

c++ - 将二维数组指针传递给 C++ 中的函数

转载 作者:行者123 更新时间:2023-11-28 01:32:00 24 4
gpt4 key购买 nike

<分区>

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

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;
}

但是,当我尝试对二维数组执行类似操作时(如下所示),我收到错误消息。

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;
}

编译器报错“error: cannot convert ‘int (*)[10][2]’ to ‘int**’ in assignment 指针=&列表;"我也愿意接受将二维数组指针传递给函数的替代方法。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com