Firstly it is a C++ code not a C code. There are used references in parameter declarations
void row_del(int**& a, int& row, int col , int k){
^^^ ^^^
and also there is used a casting of the pointer of the type void returned by the call of realloc that is not required in C.
a = (int**)realloc(a, row * sizeof(int*));
^^^^^^^
Within the function two objects of the caller, the original pointer and the integer object denoting the number of rows are modified (not their copies). So they are passed by reference.
In C passing by reference means passing an object indirectly through a pointer to it. Thus dereferencing the passed pointer a function can change the original object pointed to by the passed pointer.
That is in C the function could look like
void row_del(int ***a, int *row, int col , int k){
for(int j = 0; j < col ; j ++){
for(int i = k; i < *row - 1; i ++){
( *a )[i][j] = ( *a )[i + 1][j];
}
}
--*row;
*a = realloc( *a, *row * sizeof(int*));
}
Pay attention to that the function declaration and its definition is not good. The function realloc can return a null pointer. In this case the original array of pointers to rows can be lost. Also the function does not check whether the value of the variable k is less than the value of the expression *row. In this case the array of pointers can be reallocated and the value of rows can be decreased independent on wthere the value of the variable k is the range [0, *rows). And the types of the corresponding parameters should have unsigned integer type size_t. Instead of the return type void it would be better if the function returned a mark whether deleting a row was successful.
Also if there is only one row exists then the original pointer *a should be just set to NULL after deleting its elements and the row itself instead of calling realloc.
And moreover there is a serious bug that can produce memory leaks. Before callling realloc you need to free the memory allocated for elements of the row *row-1.
Take into account that instead of the nested for loops you could use only one for loop within the body of which you could call standard C function memcpy.
Here is a demonstration program that shows how the function could be declared and defined.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Finction row_del
// deletes k-th row in a matrix.
// The function returns:
// -1 if memory reallocation failed;
// In this case nevertheless all the rows starting from the k-th row will be overwritten
// and the pointer to the last row will be set to NULL.
// 0 if the k is out of the acceptable range or if the matrix is empty
// 1 if the k-th row was successfully deleted
int row_del( int ***a, size_t *row, size_t col, size_t k )
{
int success = k < *row;
if ( success )
{
if ( *row == 1 )
{
free( **a );
free( *a );
*a = NULL;
}
else
{
for ( size_t i = k; i < *row -1; i++ )
{
memcpy( ( *a )[i], ( *a )[i+1], col * sizeof( int ) );
}
free( ( *a )[*row - 1] );
( *a )[*row-1] = NULL;
int **tmp = realloc( *a, ( *row - 1 ) * sizeof( int * ) );
if ( tmp == NULL )
{
success = -1;
}
else
{
*a = tmp;
}
}
--*row;
}
return success;
}
int main(void)
{
size_t row = 2;
size_t col = 3;
int **a = malloc( row * sizeof( *a ) );
int value = 0;
for ( size_t i = 0; i < row; i++ )
{
a[i] = malloc( col * sizeof( **a ) );
for ( size_t j = 0; j < col; j++ )
{
a[i][j] = value++;
}
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
printf( "%d, ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
while ( row != 0 )
{
row_del( &a, &row, col, 0 );
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
printf( "%d, ", a[i][j] );
}
putchar( '\n' );
}
}
printf( "a == NULL is %s\n", a == NULL ? "true" : "false" );
}
The program output is
0, 1, 2,
3, 4, 5,
3, 4, 5,
a == NULL is true
int& row) do not exists in C, only in C++. So this is not a C program. And in C++ it is not recommended to usemallocand friends at all. But anyway a reference is indeed needed to update the pointer on the callers side.ato be anint***and do*a = realloc(*a, ...);a, so if the reallocation fails, the previously allocated data is also lost unless there was a spare pointer to it somewhere. The idiomptr = realloc(ptr, new_size);is dangerous because it loses the previous data on memory failure. This is a variant on the theme. All of that is independent of the fact that the&in the function signature means the code is not C code.reallocdoes not returnvoid, it returns a pointer. Ask yourself why.