3

I cannot create a 2D array from 2 variables (eg int arr[i][j] not allowed) so how would I create a dynamically sized 2D array?

The dimensions of the array are only known at runtime in my program. The array is to represent a grid. How would I code this in C?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • C99 allows you to declare VLA - variable-length arrays - so if you have a C99 compiler and no artificial (homework) constraints on your work, you can write precisely `int arr[i][j];`. (If you don't have a C99 compiler, it's time to get one.) With that said, how do you plan to access the array? You can either be hoping to use `arr[i][j]` to access an element, or you can be prepared to use a calculation like `arr[i*n+j]` to access it. The memory allocation patterns required are quite different for the two cases. – Jonathan Leffler Sep 17 '11 at 17:56

5 Answers5

11

First allocate an array of pointers.

/* size_x is the width of the array */
int **array = (int**)calloc(size_x, sizeof(int*));

Then allocate each column.

for(int i = 0; i < size_x; i++) 
{
    /* size_y is the height */
    array[i] = (int*)calloc(size_y, sizeof(int));
}

You can access the elements with array[i][j]. Freeing the memory is done in 'reverse' order:

for(int i = 0; i < size_x; i++) 
{
    free(array[i]);
}
free(array);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Staven
  • 3,113
  • 16
  • 20
  • I got an error with VS2010 which said that it can't cast void* to int*. So I had to explixitly cast to `int**` with `(int**)`. So that the first line becomes `int **array = (int**)calloc(size_x, sizeof(int*));` And `(int*)` at the second line. – JaBe Mar 12 '15 at 21:19
  • 1
    @JaBe You have to cast explicitly in C++. But then, why are you using calloc in C++? – Staven Mar 13 '15 at 03:56
  • I'm using C++ and calling a lot of C code which I can't change. Calling it with C++ vec does not look much better: http://stackoverflow.com/questions/6701816/how-to-use-a-stdvector-in-a-c-function – JaBe Mar 24 '15 at 07:45
  • 1
    Should the limit on that for loop be size_x not size_y? – jterm Dec 09 '16 at 21:08
8

You have to allocate a 1-dimensional array:

int* array = calloc(m*n, sizof(int));

And access it like this:

array[i*n + j]

The compiler does exactly this when accessing two-dimensional arrays, and will probably output the same code when n can be guessed at compile time.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
1

This is a FAQ on comp.lang.c (I took the liberty to add the c-faq tag), it even has a FGA (frequently given answer :-) See http://c-faq.com/aryptr/index.html, 6.16 How can I dynamically allocate a multidimensional array?

Jens
  • 69,818
  • 15
  • 125
  • 179
0

In C a multidimensional array is just an array for which each element is another array.

So you need to first allocate memory for one array (the rows). You can use the malloc() function which will return a pointer to the array.

Then you iterate through the array and for each element you allocate memory for the number of columns.

NOTE: don't forget to free the memory you manually allocate with the free() function in the same way you used malloc() to allocate it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dan Bizdadea
  • 1,292
  • 8
  • 15
0

Some of the examples show multiple (more than 2) allocations for the array; it is perfectly feasible to do it in just two allocations (error checking omitted) for an n × m array:

int **array = calloc(m, sizeof(*array));
int *data   = calloc(m * n, sizof(*data));

for (int i = 0; i < m; i++)
    array[i] = &data[i * n];


...use array[i][j]...

free(array[0]);
free(array);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278