I have a project with 2D matrices, but my professor, for whatever reason, requires that we do not use variable-length arrays. Hence, the code below doesn't compile because I don't use malloc for the 2D array. I don't know how to do it, I know it uses double pointers, but I'm only still learning C, this is quite difficult for me to grasp coming from Java. If anyone could explain how to do it, and why, it would be of a lot of help. Thank you! (code isn't finished by the way, I just need to know how to use pointers here!)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc < 2) {
printf("Need to pass in input file!");
return EXIT_SUCCESS;
}
char *filename = argv[1];
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
perror(filename);
return 0;
}
int size;
fscanf(fp,"%d", &size);
int matrix[size][size];
int i;
int j;
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++) {
int num;
fscanf(fp,"%d",&num);
matrix[i][j] = num;
}
}
// printf("The size of the matrix is: %d", size);
fclose(fp);
return EXIT_SUCCESS;
}