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
407k213 gold badges579 silver badges892 bronze badges
asked Sep 17, 2011 at 17:39
1
  • 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. Commented Sep 17, 2011 at 17:56

5 Answers 5

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
52.1k193 gold badges61 silver badges101 bronze badges
answered Sep 17, 2011 at 17:48
Sign up to request clarification or add additional context in comments.

4 Comments

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 You have to cast explicitly in C++. But then, why are you using calloc in C++?
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: stackoverflow.com/questions/6701816/…
Should the limit on that for loop be size_x not size_y?
7

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.

answered Sep 17, 2011 at 17:42

Comments

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?

answered Sep 17, 2011 at 18:03

Comments

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
759k145 gold badges961 silver badges1.3k bronze badges
answered Sep 17, 2011 at 17:44

Comments

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);
answered Sep 17, 2011 at 18:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.