使用C在内存中实现二维数组
Implementaion of 2-D array in meomory using C
实际问题 - 在矩阵中找到鞍点。
我的函数调用 - find_saddle_point((Arr,rows);
我的函数原型 - void find_saddle_point(int *,int);
初始化 -
int rows , column ;
int Arr[rows][column];
警告-
警告:从不兼容的指针类型
传递“find_saddle_point”的参数 1
我的临时解决方案 - find_saddle_point(((int *)Arr,rows);
You declared a two dimensional array.
int Arr[rows][column];
Used in expressions like in this function call
find_saddle_point((Arr,rows);
it is implicitly converted to pointer to its first element and has the type int ( * )[column]
.
However the corresponding parameter has the type int *
and there is no implicit conversion from the type int ( * )[column]
to the type int *
.
void find_saddle_point(int *,int);
So the compiler issues an error.
As for your temporary "solution"
find_saddle_point(((int *)Arr,rows);
then it can result in undefined behavior.
Declare the function like
void find_saddle_point( int, int, int [][*]);
In its definition you shall specify the names of parameters.
void find_saddle_point( int row, int column, int [][column])
{
// ...
}
Also for dimensions of arrays it is better to use the type size_t
instead of the type int
.
Here is a demonstrative program.
#include <stdio.h>
void find_saddle_point( size_t, size_t, int a[][*] );
void fill( size_t rows, size_t cols, int a[][cols] )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ )
{
a[i][j] = i * cols + j;
}
}
}
int main(void)
{
size_t rows = 3;
size_t cols = 5;
int a[rows][cols];
fill( rows, cols, a );
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ )
{
printf( "%2d ", a[i][j] );
}
putchar( '\n' );
}
return 0;
}
Its output is
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
Of course the first function declaration in this simple program is redundant and shown only to demonstrate how a function with a parameter that has a type of a variable-length array can be declared.
实际问题 - 在矩阵中找到鞍点。
我的函数调用 - find_saddle_point((Arr,rows);
我的函数原型 - void find_saddle_point(int *,int);
初始化 -
int rows , column ;
int Arr[rows][column];
警告- 警告:从不兼容的指针类型
传递“find_saddle_point”的参数 1我的临时解决方案 - find_saddle_point(((int *)Arr,rows);
You declared a two dimensional array.
int Arr[rows][column];
Used in expressions like in this function call
find_saddle_point((Arr,rows);
it is implicitly converted to pointer to its first element and has the type int ( * )[column]
.
However the corresponding parameter has the type int *
and there is no implicit conversion from the type int ( * )[column]
to the type int *
.
void find_saddle_point(int *,int);
So the compiler issues an error.
As for your temporary "solution"
find_saddle_point(((int *)Arr,rows);
then it can result in undefined behavior.
Declare the function like
void find_saddle_point( int, int, int [][*]);
In its definition you shall specify the names of parameters.
void find_saddle_point( int row, int column, int [][column])
{
// ...
}
Also for dimensions of arrays it is better to use the type size_t
instead of the type int
.
Here is a demonstrative program.
#include <stdio.h>
void find_saddle_point( size_t, size_t, int a[][*] );
void fill( size_t rows, size_t cols, int a[][cols] )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ )
{
a[i][j] = i * cols + j;
}
}
}
int main(void)
{
size_t rows = 3;
size_t cols = 5;
int a[rows][cols];
fill( rows, cols, a );
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ )
{
printf( "%2d ", a[i][j] );
}
putchar( '\n' );
}
return 0;
}
Its output is
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
Of course the first function declaration in this simple program is redundant and shown only to demonstrate how a function with a parameter that has a type of a variable-length array can be declared.