1

I would like to create a function which increase a given 2d dynamic int array with one row. I looked several website, guide, tutorial, but all is different, so I'm very confused now.

The 2d array has 2 fixed columns.

My code is here:

int length=1;
void arrayinc(int** array, int x0, int x1)
{
    if (array == NULL)
        malloc(array, sizeof(int[2]));
    else
        realloc(array, (++length)*sizeof(int[2]));

    array[length-1][0]=x0;
    array[length-1][1]=x1;

    free(array);
}

int main()
{
    int** array=NULL;
    arrayinc(&array, 1, 2);
    // I will do some stuff after the increase
}

I hope someone can help me, and explain how it really works!

Sorry for my english and bad malloc/realloc knowlage.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Gregori
  • 85
  • 7
  • 2
    Duplicate of [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). It is perfectly normal to be confused about this topic, since there's so many crap books and crap teachers out there, teaching you bad practice. – Lundin Feb 27 '18 at 15:12
  • `malloc(array, sizeof(int[2]));` um, no. – n. m. could be an AI Feb 28 '18 at 14:58
  • The archetypal function that resizes a dynamic array is realloc. Look at its signature very carefully. Why does it have this signature and not any other? Why does it return what it does? Should your own function drastically depart from the way realloc is working? If so, why? – n. m. could be an AI Feb 28 '18 at 15:02

1 Answers1

1

Function parameters are its local variables. So within the function you deal with a copy of the original argument.

At least the parameter shall be declared like

int*** array

If the number of columns is a compile-time constant then the function can be defined the following way.

#include <stdio.h>
#include <stdlib.h>

#define N   2

size_t arrayinc( int ( **array )[N], size_t n, int x0, int x1)
{
    int ( *tmp )[N] = realloc( *array, ( n + 1 ) * sizeof( int[N] ) );

    if ( tmp )
    {
        *array = tmp;
        ( *array )[n][0] = x0;
        ( *array )[n][1] = x1;
        ++n;
    }

    return n;
}

int main(void) 
{
    int ( *array )[N] = NULL;
    size_t n = 0;

    for ( size_t i = 0; i < 10; i++ )
    {
        n = arrayinc( &array, n, ( int )( 2 * i ), ( int )( 2 * i + 1 ) );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d\t%d\n", array[i][0], array[i][1] );
    }

    free( array );

    return 0;
}

The program output is

0   1
2   3
4   5
6   7
8   9
10  11
12  13
14  15
16  17
18  19
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you! But can we do it all in a single function? And I also tried to create a separated print function, but it show wrong numbers. The code is: void show(int (**array)[2], size_t n) { for (int y = 0; y < n; y++) { printf("%2d -> %2d\n", array[y][0], array[y][1]); } } – Gregori Feb 27 '18 at 16:47
  • @Gregori To print the array just declare the function like void show( int ( *array )[2], size_t n ); and call it like show( array, n ); – Vlad from Moscow Feb 28 '18 at 12:40