There are several ways to dynamically allocate memory for an NxM array. Here are two:
You can declare a pointer to an M-element array, and then malloc N instances of it:
double (*podrucje)[11] = malloc(sizeof *podrucje * 123);
As of C89, you don't need to cast the result of malloc, and the practice is discouraged. Also, note that the operand to sizeof is the expression *podrucje; this gives me the same result as sizeof (double) * 11.
You would index this array as podrucje[i][j] like any other 2D array. podrucje[i] implicitly dereferences the pointer (remember that a[i] is equivalent to *(a + i)) so you don't have to do anything funky with it.
You would use it in a function as follows:
void init(double (*podrucje)[11], size_t rows) { size_t i, j; for (i = 0; i < rows; i++) for (j = 0; j < 11; j++) podrucje[i][j] = 0.0; }
which would be called as
init(podrucje, 123);
The drawback to this method is that the function can only operate on Nx11 arrays; if you're using a C99 compiler or a C2011 compiler that supports variable length arrays, you could specify the number of columns as a runtime variable:
void foo(void) { size_t rows = 123, cols = 11; double (*podrucje)[cols] = malloc(sizeof *podrucje * rows); if (podrucje) init(cols, podrucje, rows); ... } // cols must be declared before it can be used // in an array declarator // void init(size_t cols, double(*podrucje)[cols], size_t rows) { size_t i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) podrucje[i][j] = 0.0; }
When you're done with the array, deallocate it as follows:
free(podrucje);
The other approach is to allocate each row separately, as follows:
size_t rows = 123, cols = 11; double **podrucje = malloc(sizeof *podrucje * rows); if (!podrucje) { // malloc failed; handle allocation error here } else { size_t i; for (i = 0; i < rows; i++) { podrucje[i] = malloc(sizeof *podrucje[i] * cols); if (!podrucje[i]) { // malloc failed; handle allocation error here } } }
And you would use it in a function as follows:
void foo() { double **podrucje; // allocate array as above init(foo, rows, cols); ... } void init(double **podrucje, size_t rows, size_t cols) { size_t i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) podrucje[i][j] = 0.0; }
When you're finished with the array, deallocate it as follows:
for(i = 0; i < rows; i++) free(podrucje[i]); free(podrucje);
The first method allocates memory as a single, contiguous block; the second allocates it in a series of smaller, discontinuous chunks. If your array is especially big or your heap especially fragmented, the first method may fail where the second will succeed. If you're working with a compiler that doesn't support variable-length arrays, the first method is much less flexible, because the number of columns must be specified at compile time.
How could the same indexing method work for both forms?
In the first case, each podrucje[i] is an 11-element array of double; indexing it with j works like any other array. In the second case, each podrucje[i] is a pointer to double. Since a[i] is evaluated as *(a + i), array indexing works on pointer expressions just as well as array expressions.