# Thread: Declare 2D array without size, give size later

1. ## Declare 2D array without size, give size later

I've got an interesting problem.
I can't determine the dimensions of the 2D array until I get the information from the if or the else if, so I would like to declare it before that so it is visible from both the if and the else if. I obviously can't create it inside the if or else if because then it would be out of scope after exiting the if else statement.
I can declare it with float **matrix beforehand but I wish to be able to do it like
Code:
`float *matrix[];`
to make it compatible with other functions. Is this a way to get around this problem?
Code:
```if (strcmp(argv[i], "a") == 0)			{

sscanf(argv[i + 1], "%d", &size);
float *matrix[size];
for (j = 0; j < size; j++)
{
matrix[j] = (float *)malloc(sizeof(float) * size);
}
fill_matrix(matrix,size,size);
}
else if (strcmp(argv[i], "-d") == 0)
{
/*read matrix dimensions and components from file here
////////////////////////////////////////////////////
*/
float *matrix[size];
for (j = 0; j < size; j++)
{
matrix[j] = (float *)malloc(sizeof(float) * size);
}
fill_matrix(matrix,size,size);
}
else{}```

2. I think the easiest way is to declare a pointer to a float and allocate the array as needed. For example:

Code:
```// lets say m and n are provided as
// the array dimensions, like in matrix[m][n].

float *matrix;
matrix = malloc( sizeof(float) * m * n );

// you can use:
matrix[a*n+b] = 1.0f; // same as matrix[a][b] = 1.0f;```
But, of course, you need to verify if 0 <= a < m and 0 <= b < n.

3. You're making a jagged array, which is probably not what you want. Instead, try a C99 dynamic array.

Code:
```#include <stdio.h>

int main(int argc, char* argv[])
{
int size;
sscanf(argv[1], "%d", &size);

int matrix[size][size];
matrix[size/2][size/2] = 1;  // Do stuff

return 0;
}```
This stores the array in contiguous memory on the stack and since the width of the rows is know, it can be indexed normally. It's stored on the stack, though. Instead, you might want to do something like this.

Code:
```#include <stdio.h>

int main(int argc, char* argv[])
{
int size;
sscanf(argv[1], "%d", &size);

int (*matrix)[size][size] = malloc(sizeof(int) * size * size);
matrix[size/2][size/2] = 1;  // Do stuff

return 0;
}```
This matrix lives on the heap in contiguous memory and is very usable. However, the type is dynamic. You can't pass this to a function very easily without losing type information. It's less flexible than a normal array in that way. So you'll probably end up with just a plain old int* and address using y*size+x. You might as well wrap the pointer and size in a struct while you're at it. And then you'll probably want a set of functions to operate on these matrices.

Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int *mat;
int size;
} Matrix;

void mat_clear(Matrix m)
{
for(int i = 0; i < m.size * m.size; i++)
m.mat[i] = 0;
}

Matrix mat_create(int size)
{
Matrix m;
m.size = size;
m.mat = malloc(sizeof(int) * size * size);
mat_clear(m);

return m;
}

void mat_print(Matrix m)
{
for(int y = 0; y < m.size; y++)
{
for(int x = 0; x < m.size; x++)
printf("%d ", m.mat[y*m.size+x]);
printf("\n");
}
}

int main(int argc, char *argv[])
{
int size;
sscanf(argv[1], "%d", &size);

Matrix m = mat_create(size);
m.mat[(size/2)*size+(size/2)] = 1;
mat_print(m);

return 0;
}```

4. That works, thanks for the help and useful information.

5. I would also say that using a struct is a better way to go

However: Don't forget to free memory that you have malloc'd - You wouldn't want a memory leak

i.e.
Code:
```void mat_distroy(Matrix *m)
{
free (m->mat);
m->size = 0;
}```