read from a file using dynamic memory
void print2d(){ FILE *fp; int i, j, l=3, c=3; int *ptr; ptr = malloc(l * c * sizeof(ptr)); if (ptr == NULL){ printf("Memory not allocated"); exit(1); } if (!(fp = fopen("arr.txt", "r"))){ printf("Can't open arr.txt"); exit(2); } i=0; j=0; while(!feof(fp)){ fscanf(fp, "%d", (ptr+i*c + j)); i++; if (i==3){ i = 0; j ++; } } for (i = 0; i<l; i++){ for (j = 0; j<c; j++){ printf("%3d", *(ptr + i * c + j)); } } } int main() { print2d(); return 0; } I need to create a function that read a matrix from a file then print the file. Using dynamic memory allocation. This is my attempt and I don't know why is not working.