使用指针将2D数组传递给函数 [英] Passing 2D array to function using pointer
本文介绍了使用指针将2D数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我在传递2d数组时遇到错误。
它给我以下错误
错误C2664:无法转换参数1从int [3] [4]到int *
我无法弄清楚我在哪里弄错了。这是我的代码
Hi
I am getting and error while passing 2d array to function.
It gives me following error
error C2664: cannot convert parameter 1 from int [3][4] to int*
I am not able to figure out where i am making mistake. Here is my code
#include<iostream>
#include<conio.h>
using namespace std;
class Demo
{
public:
void TwoDArrPass1(int *arr,int col,int row)
{
}
};
void main()
{
Demo dd;
int Arr[3][4] = { {1,2,3,4},
{10,20,30,40},
{100,200,300,400}
};
dd.TwoDArrPass1(Arr,3,4);
_getch();
}
我的尝试:
我试图搜索但没有弄清楚我的错误。
预先感谢您的帮助
What I have tried:
I have tried to search but not getting any clear idea about my mistake.
Thanks in advance for help
推荐答案
这已被多次询问,因此可以通过网络搜索回答。
所以我已经为你做了这件事,而不是重复已经说过的话:
将2D数组传递给C ++函数 - Stack Overflow [ ^ ]
在C ++中将2d数组传递给指针 - C ++论坛 [ ^ ]
指针指针和指针引用 [ ^ ]
如何将二维数组传递给函数(C ++) [ ^ ]
This has been asked multiple times and can be answered therefore by searching the web.
So I have done this for you instead of repeating something said already:
Passing a 2D array to a C++ function - Stack Overflow[^]
Passing 2d array to pointer in C++ - C++ Forum[^]
Pointer to Pointer and Reference to Pointer[^]
How to Pass a Two Dimensional Array to a Function (C++)[^]
演员可以做到这一点,例如
A cast would do the trick, e.g.
#include<iostream>
using namespace std;
class Demo
{
public:
void TwoDArrPass1(int * arr ,int col,int row)
{
for ( int c = 0; c<col; ++c)
{
for (int r=0; r<row; ++r)
{
cout << arr[row*c+r] << " ";
}
cout << endl;
}
}
};
int main()
{
Demo dd;
int Arr[3][4] = { {1,2,3,4},
{10,20,30,40},
{100,200,300,400}
};
dd.TwoDArrPass1(reinterpret_cast<int *>(Arr),3,4);
}
这篇关于使用指针将2D数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文