For my homework, I have to read data from an input file and to store it in a double dimensional array and then pass this array to another function. This is what I have tried so far, but I don’t know when I call this function in main it gives the error:
Access violation writing location 0x00000000.
I have tried to dynamically allocate memory and it gives the same error. What I am doing wrong?
The last update of the code:
#include<stdio.h>
#include<stdlib.h>
int *a[2];
void getData(void)
{
FILE *fp = fopen("input.txt", "r");
int number;
fscanf(fp, "%d", &number);
for (int i = 0; i < number; i++)
{
a[i]=(int*)malloc(number * sizeof (int));
fscanf(fp, "%d %d", &a[i][0], &a[i][1]);
}
fclose(fp);
}
int main()
{
getData();
for(int i=0;i<8;i++)
{
printf("%d %d\n",a[i][0],a[i][1]);
}
}
You need to allocate memory for
aBefore the for loop