[英]Use function to create and free 2D array in C
I practice the 2D dynamic array with reference to the URL below: https://thispointer.com/allocating-and-deallocating-2d-arrays-dynamically-in-c-and-c/ 我参考以下URL来练习2D动态数组: https : //thispointer.com/allocating-and-deallocating-2d-arrays-dynamically-in-c-and-c/
My code: 我的代码:
#include <stdio.h>
#include <stdlib.h>
int** create_2d_arr(int row_size,int colum_size)
{
int** array = (int**)malloc(sizeof(int*)*row_size);
for (int i = 0; i < row_size; i++)
array[i] = (int*)malloc(sizeof(int)*colum_size);
return array;
}
void free_2d_arr(int** matrix,int row_size, int colum_size) {
for (int i = 0; i < row_size; i++) {
free(matrix[i]);
}
free(matrix);
}
int main(int argc, char const *argv[])
{
int row=3,cloum=2;
int** arr_2d = create_2d_arr(row,cloum);
arr_2d[0,0]=4;
arr_2d[0,1]=5;
arr_2d[1,0]=6;
arr_2d[1,1]=7;
arr_2d[2,0]=8;
arr_2d[2,1]=9;
for(int i=0;i<row;i++)
for(int j=0;j<cloum;j++)
printf("arr_2d[%d,%d] = %d \n",i,j,arr_2d[i,j]);
free_2d_arr(arr_2d,row,cloum);
return 0;
}
However, there are errors when executing after compilation: 但是,编译后执行时会出现错误:
arr_2d[0,0] = 8
arr_2d[0,1] = 9
arr_2d[1,0] = 8
arr_2d[1,1] = 9
arr_2d[2,0] = 8
arr_2d[2,1] = 9
[1] 9300 segmentation fault (core dumped) ./t
Only arr_2d[2,0]=8 arr_2d[2,1]=9 are correct. 仅arr_2d [2,0] = 8 arr_2d [2,1] = 9是正确的。 I don't understand where my code is wrong.
我不明白我的代码在哪里错误。 Does anyone help me?
有人帮我吗?
thanks for your replies. 感谢您的回复。 but after I modify
arr_2d[2,0]=8
to a rr_2d[2][0]=8
... 但在我将
arr_2d[2,0]=8
修改为rr_2d[2][0]=8
...
result of printf is
arr_2d[0][0] = -267545984
arr_2d[0][1] = -267545952
arr_2d[1][0] = -267545984
t.c:38:47: warning: expression result unused [-Wunused-value]
printf("arr_2d[%d,%d] = %d \n",i,j,arr_2d[i,j]);
^
t.c:38:40: warning: format specifies type 'int' but the argument has type
'int *' [-Wformat]
printf("arr_2d[%d,%d] = %d \n",i,j,arr_2d[i,j]);
~~ ^~~~~~~~~~~
2 warnings generated.
my compiler is clang,even if I use gcc 我的编译器是clang,即使我使用gcc
========= =========
After modify: 修改后:
printf("arr_2d[%d,%d] = %d \n",i,j,arr_2d[i,j]);
=> =>
printf("arr_2d[%d,%d] = %d \n",i,j,arr_2d[i],[j]);
It work normally. 它正常工作。 Thank everyone very much.
非常感谢大家。
you can't access array's value like this 你不能像这样访问数组的值
arr_2d[x,y];
you have to use this type of syntax 您必须使用这种类型的语法
arr_2d[x][y];
arr_2d[x,y]
is equivalent to arr_2d[y]
arr_2d[x,y]
等同于arr_2d[y]
use arr_2d[x][y]
in assignment and access 在分配和访问中使用
arr_2d[x][y]
In C (or C++) a,b,c .., z
compute a then b .. then z and returns the value of z 在C(或C ++)
a,b,c .., z
计算A然后B ..然后z和返回z的值
So : 因此:
#include <stdio.h>
#include <stdlib.h>
int** create_2d_arr(int row_size,int colum_size)
{
int** array = (int**)malloc(sizeof(int*)*row_size);
int i;
for ( i = 0; i < row_size; i++)
array[i] = (int*)malloc(sizeof(int)*colum_size);
return array;
}
void free_2d_arr(int** matrix,int row_size, int colum_size) {
int i;
for ( i = 0; i < row_size; i++) {
free(matrix[i]);
}
free(matrix);
}
int main(int argc, char const *argv[])
{
int row=3,cloum=2;
int** arr_2d = create_2d_arr(row,cloum);
arr_2d[0][0]=4;
arr_2d[0][1]=5;
arr_2d[1][0]=6;
arr_2d[1][1]=7;
arr_2d[2][0]=8;
arr_2d[2][1]=9;
int i,j;
for( i=0;i<row;i++)
for( j=0;j<cloum;j++)
printf("arr_2d[%d][%d] = %d \n",i,j,arr_2d[i][j]);
free_2d_arr(arr_2d,row,cloum);
return 0;
}
The result is : 结果是:
arr_2d[0][0] = 4
arr_2d[0][1] = 5
arr_2d[1][0] = 6
arr_2d[1][1] = 7
arr_2d[2][0] = 8
arr_2d[2][1] = 9
The correct syntax is 正确的语法是
arr_2d[i][j] = n;
I have never seen something like arr_2d[i,j]
and would be great-full if someone explained it to us. 我从未见过像
arr_2d[i,j]
这样的东西arr_2d[i,j]
如果有人向我们解释它,我会感到非常arr_2d[i,j]
。
My guess is that it's equivalent to arr_2d[j]
but I'm not sure. 我的猜测是它等同于
arr_2d[j]
但我不确定。
EDIT: precision: you should change ALL your arr_2d[i,j]
(like arr_2d[0,0]
) to the new syntax 编辑:精度:您应将所有
arr_2d[i,j]
(如arr_2d[0,0]
)更改为新语法
the following proposed code: 以下建议的代码:
size_t
rather than int
to avoid compiler warnings about conversions between int
and size_t
, especially in the calls to calloc()
and malloc()
size_t
而不是int
以避免编译器警告int
和size_t
之间的转换,尤其是在对calloc()
和malloc()
的调用中 And now, the proposed code: 现在,建议的代码:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
// prototypes
void free_2d_arr( int** matrix, size_t row_size, size_t colum_size );
int** create_2d_arr( size_t row_size, size_t colum_size )
{
int** array = calloc( row_size, sizeof( int* ) );
if( !array )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
for ( size_t i = 0; i < row_size; i++ )
{
array[ i ] = malloc( sizeof(int) * colum_size );
if( !array[ i ] )
{
perror( "malloc failed" );
free_2d_arr( array, row_size, colum_size );
exit( EXIT_FAILURE );
}
}
return array;
}
void free_2d_arr( int** matrix, size_t row_size, size_t colum_size )
{
(void)colum_size;
for ( size_t i = 0; i < row_size; i++ )
{
free( matrix[ i ] );
}
free( matrix );
}
int main( void )
{
size_t row = 3;
size_t cloum = 2;
int** arr_2d = create_2d_arr( row, cloum );
arr_2d[ 0 ][ 0 ] = 4;
arr_2d[ 0 ][ 1 ] = 5;
arr_2d[ 1 ][ 0 ] = 6;
arr_2d[ 1 ][ 1 ] = 7;
arr_2d[ 2 ][ 0 ] = 8;
arr_2d[ 2 ][ 1 ] = 9;
for( size_t i=0; i < row; i++ )
{
for( size_t j=0; j < cloum; j++ )
{
printf( "arr_2d[%lu,%lu] = %d \n", i, j, arr_2d[ i ][ j ] );
}
}
free_2d_arr( arr_2d, row, cloum );
return 0;
}
a run of the proposed code results in: 建议代码的运行导致:
arr_2d[0,0] = 4
arr_2d[0,1] = 5
arr_2d[1,0] = 6
arr_2d[1,1] = 7
arr_2d[2,0] = 8
arr_2d[2,1] = 9
Note, however, that the statements: 但是请注意,以下语句:
size_t row = 3;
size_t cloum = 2;
are really parameters to the program and the 2
and 3
are constants and 'magic' numbers. 是程序的真正参数,而
2
和3
是常数和“魔术”数。 'magic' numbers are numbers with no basis. “魔术”数字是没有基础的数字。 'magic' numbers make the code more difficult to understand, debug, etc. Suggest using
#define
statements or a enum
statement to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code “魔术”数字使代码更难以理解,调试等。建议使用
#define
语句或enum
语句为这些“魔术”数字赋予有意义的名称,然后在整个代码中使用这些有意义的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.