I was using malloc() to create a 2d array to store data from the stdin. I wanted to test it out before coding specifically what is required to get a general idea. I found the following code online
int r, c;
scanf("%d%d", &r, &c);
int *arr = (int *)malloc(r * c * sizeof(int));
int i, j, count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
*(arr + i*c + j) = ++count;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", *(arr + i*c + j));
The online code had hardcoded values for r and c so I changed it to a scanf to make it similar to what I would be needing later. It works well but I do not understand why I cannot use arr[i][j] instead of *(arr + i *c +j) in my code. When I tried using it i got the following error: error: subscripted value is neither array nor pointer nor vector
My understanding was that pointers and arrays are interchangeable, where am I going wrong?