I want to allocate a 2D array of my custom type "cell", which is a structure. However, I am doing something wrong, see my code below. Could you please tell me where my mistake is?
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int variable_1;
int variable_2;
int variable_3;
} cell;
void initialiseArray(unsigned long rows, unsigned long columns, cell array[rows][columns])
{
for (int i = 0; i < rows; i = i + 1)
for (int j = 0; j < columns; j = j + 1)
{
array[i][j].variable_1 = 0;
array[i][j].variable_2 = 0;
array[i][j].variable_3 = 0;
}
}
int main()
{
unsigned long rows = 200;
unsigned long columns = 250;
cell* array[rows];
for (unsigned long i = 0; i < rows; i = i + 1)
array[i] = malloc(columns * sizeof(cell));
if (array == NULL)
{
printf("Error in ""main"": Memory could not be allocated.\n");
return 3;
}
initialiseArray(rows, columns, array);
return 0;
}