Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
#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
Posted
Updated 15-Jun-16 1:48am
Comments
Member 14507089 8-Jul-19 8:26am    
please ans it

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++)[^]
 
Share this answer
 
A cast would do the trick, e.g.
C++
 #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);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900