If you want to allocate memory you will eventually need to know the amount you need, there is no two ways about it, you can however declare a pointer to array of a fixed number of columns, and later when you know the number of rows, allocate the space you need.
Sample code with comments:
int main()
{
//pointer to an array with 10 columns
//at this point the number of rows is not required
int(*arr)[10];
//to allocate memory you need to know how much of it you need
//there is no two ways about it, so when you eventually know
//the number of rows you can allocate memory for it
arr = malloc(sizeof *arr * 5); // 5 is the number of rows
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 10; j++)
{
arr[i][j] = 5;
}
}
free(arr); //free the memory when your are done using arr
}
Another option is to reallocate memory as you go, for each new row:
Live demo
for (int i = 0; i < 5; i++)
{
//allocate memory for each new row
arr = realloc(arr ,sizeof *arr * (i + 1));
if(arr == NULL){
perror("malloc");
return EXIT_FAILURE;
}
for (int j = 0; j < 10; j++)
{
arr[i][j] = 5;
}
}
Test printing the array:
for (int i = 0; i < 5; i++) //test print the array
{
for (int j = 0; j < 10; j++)
{
printf("%d", arr[i][j]);
}
putchar('\n');
}
Will output in both cases:
5555555555
5555555555
5555555555
5555555555
5555555555