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;
}