I am trying to declare a 2d array with a integer as the size the integer is sent from another function.
Is this possible?
because i can't get it to work!
I am trying to declare a 2d array with a integer as the size the integer is sent from another function.
Is this possible?
because i can't get it to work!
> Is this possible?
It is in C++
And will be when your compiler supports the latest C99 standard
But until then, you need this
Code:#include <stdlib.h> void foo ( int size ) { int **arr; int i; // allocate a 2D array arr = malloc( sizeof(int*) * size ); for ( i = 0 ; i < size ; i++ ) arr[i] = malloc( sizeof(int) * size ); // use arr as arr[row][col] // as if you had declared int arr[size][size]; // free the allocated array for ( i = 0 ; i < size ; i++ ) free( arr[i] ); free( arr ); }
can only be achieved using dynamic allocation
i.e. using malloc/free for C
or using new/delete for C++