[英]Cannot pass an 2D array in C++ which gives error cannot convert int(*) to int*
The code is代码是
#include<iostream>
using namespace std;
void lookup(int* main,int row,int col,int num,int* up)
{
int i,j;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if((row==i && (col==j+1 || col==j-1)) || (col=j && (row==i+1 || row==i-1)))//neighbour condition
{
if((*(main+i*num)+j)==1)
{
if((*(up+(i*num)+j)==1))
{
*((up+num*i)+j)=1;
lookup(main,i,j,num,up);
}
}
}
}
}
}
int main()
{
// flipping array b, input array a, we have marked array b
int n,i,j,botr,botc,place;
cin>>n;
int a[n][n],b[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
b[i][j]=0;
}
}
cin>>botr>>botc;
b[botr][botc]=1;
lookup(a,botr,botc,n,b);
//seeing which are 1 in b
place=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i][j]==1)
place++;
}
}
cout<<place;
}
and the error I get is我得到的错误是
cannot convert 'int (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'int*' for argument '1' to 'void lookup(int*, int, int, int, int*)'|
So I am given a matrix with index starting from 0,0.所以我得到了一个索引从 0,0 开始的矩阵。 The matrix is of values 0 and 1. Now when I am given any particular position(with the value 1 in that position) I have to find the all possible distinct points where I can go and the value is 1, given I can only move in adjacent ways and the ways does not contain 0.
矩阵的值为 0 和 1。现在,当我被赋予任何特定位置(该位置的值为 1)时,我必须找到所有可能的不同点,我可以 go 并且值为 1,因为我只能移动在相邻的方式中,并且方式不包含0。
Thanks.谢谢。
Your a
is not an array of integers, but rather an array of an array of integers.您
a
不是整数数组,而是整数数组的数组。 Hence, it does not convert to a pointer to an integer.因此,它不会转换为指向 integer 的指针。
From what I understand of your code, you can use &a[0]
, as a[0]
is an array of integers.根据我对您的代码的理解,您可以使用
&a[0]
,因为a[0]
是一个整数数组。
So Thanks for the reply, but I couldn't implement your method.所以感谢您的回复,但我无法实施您的方法。 Anyway here is the workaround I figured.
无论如何,这是我想出的解决方法。 So I defined two extra variables
L1
and L2
of type int*
.所以我定义了两个额外的变量
L1
和L2
类型的int*
。 Defined L1=a[0][0], L2=b[0][0]
.定义
L1=a[0][0], L2=b[0][0]
。 Then passed them in the lookup()
function and worked like a charm.然后将它们传递给
lookup()
function 并像魅力一样工作。 I will always pass 2d or more dimensional array this way from now on.从现在开始,我将始终以这种方式传递二维或更多维数组。 Thanks for help guys.
谢谢你们的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.