Reputation: 55
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/
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. 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 arr_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
=========
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.
Upvotes: 0
Views: 171
Reputation: 16540
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()
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. '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
Upvotes: 0
Reputation: 32596
arr_2d[x,y]
is equivalent to arr_2d[y]
use arr_2d[x][y]
in assignment and access
In C (or C++) a,b,c .., z
compute a then b .. then z and returns the value of 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
Upvotes: 1
Reputation: 424
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.
My guess is that it's equivalent to arr_2d[j]
but I'm not sure.
EDIT: precision: you should change ALL your arr_2d[i,j]
(like arr_2d[0,0]
) to the new syntax
Upvotes: 0
Reputation:
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];
Upvotes: 2