更新时间:2023-11-30 15:39:34
您提供的代码很少有问题,我将一一介绍给您,
Your provided code have few issues i will explain them to you one by one ,
要将多维数组传递给函数,我将向您展示2种方法做到这一点
To pass multidimensional arrays to functions , I will show you 2 ways to do that
1)将多维数组作为一维数组传递
之所以可行,是因为我们知道数组如何在内存中表示,而我们对表示的了解正是使C / C ++中的指针如此强大的工具。
阅读此。
1) Passing multidimensional arrays as single dimensional arrays This works because we know how array is represented in the memory , and our this knowledge of representation is what makes pointers in C/C++ such a powerful tool . Read this answer to get a better picture of representation of array in memory.
元素(此处为ints)的填充方式如图片中的之字形线所描述。
因此,在我们的示例数组 int arr [5] [5]
中,
第一行的第二个元素( arr [ 0] [1]
)可以通过 *(arr + 0 * 5 + 1)
进行访问,就像 arr
给出数组的基地址,类似地, arr [4] [3] ) $ c> *(arr + 4 * 5 + 3),这里我们将行索引乘以5,因为每行有5个元素(即列数),有了这些知识,我们可以
Arrays are represented linearly and contiguously in memory , thus if define array as arr[5][5]
, we are telling compilers that we need a memory block having sufficient space for storing 5*5 = 25 int data types . And it's also worth to know that arrays are represented in row major form , read this to learn more about row major form.
.
Elements(here ints) are filled in the way as described by the zig-zag line in the picture .
Thus in our example array int arr[5][5]
,
2nd element of the 1st row(arr[0][1]
) can be accessed by *(arr+0*5+1)
, as arr
gives the base address of the array , similarly , 4th element of 5th row (arr[4][3]
) can be accessed by *(arr+4*5+3)
, here we are multiplying row index by 5 because each row have 5 elements(that is number of columns) , with this knowledge in mind we can write code to access array elements of a matrix in the following way
在调用函数时,强制转换 arr
到(int *)
是必要的,因为最初的arr类型是 int(*)[3]
,
void display(int *arr,int r,int c)
{
for(unsigned i=0;i<r;++i)
{
for(unsigned j=0;j<c;++j)
{
cout<<*(arr+i*c+j)<<ends;
}
cout<<endl;
}
}
const unsigned ROW=3,COL=3;
int main()
{
int arr[ROW][COL]={1,2,3,
4,5,6,
7,8,9
};
display((int *)arr,ROW,COL);
}
2)将多维数组作为指针传递给函数参数中的数组。
While calling the function , casting arr
to (int *)
is necessary because originally arr type is int (*)[3]
, that is pointer to an int array of 3 elements .
2) p>
2) Passing multidimensional array as pointer to an array in the function argument .
const unsigned ROW=3,COL=3;
void display(int (*arr)[COL],int r,int c)
{
for(unsigned i=0;i<r;++i)
{
for(unsigned j=0;j<c;++j)
{
cout<<arr[i][j]<<ends;
}
cout<<endl;
}
}
int main()
{
int arr[ROW][COL]={1,2,3,
4,5,6,
7,8,9
};
display(arr,ROW,COL);
}
在此,无需强制转换 arr
在您的代码中,您并没有在意数组索引超出范围,如果您的数组是 int arr [5] [5]
并且您尝试访问 arr [-1] [ 5]
或 arr [5] [3]
,结果将是不确定的,这就是代码崩溃到系统都会发生的一切
In your code you are not taking care of array index going out of bounds , if your array is int arr[5][5]
and you try to access arr[-1][5]
or arr[5][3]
, the result will be undefined , that is anything can happen from your code just crashing to your system going up in flames(just a metaphor) .
记住这些事情,满足您需求的有效代码就是
Keeping these things in mind , a working code satisfying your needs is
int count(int *t, int r, int c)
{
int i, j, result = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
if(i!=r-1)
{
if(*(t+i*c+j)==*(t+(i+1)*c+j))
++result;
if(j!=r-1)
{
if(*(t+i*c+j)==*(t+(i+1)*c+j+1))
++result;
}
if(j!=0)
{
if(*(t+i*c+j)==*(t+(i+1)*c+j-1))
++result;
}
}
if(j!=c-1)
{
if(*(t+i*c+j)==*(t+i*c+j+1))
++result;
}
}
}
return result;
}
const unsigned ROW=3,COL=3;
int main()
{
int arr[ROW][COL]={1,1,2,
1,2,3,
4,5,2
};
cout<<count((int *)(arr),ROW,COL)<<endl;
}