My teacher told me that int **array is not a 2D array, it is just a pointer to a pointer to an integer. Now, in one of my projects I have to dynamically allocate a 2D array of structs, and this is how it is done:
struct cell **array2d = (struct x **)calloc(rows, sizeof(struct x *));
for (int i = 0; i < columns; i++) {
array2d[i] = (struct x *)calloc(j, sizeof(struct x));
}
But here we return a pointer to a pointer to the struct, so how is this a 2D array?
Before using dynamic allocation, I had a statically allocated array of the form: array2d[][]
Now that I replaced it by dynamic allocation, I also replaced array2d[][] by **array2d.
Every function that takes array2d[i][j] als argument now returns an error saying the types don't match.
Could someone explain me what is happening here? What is the difference between **array and array[m][n] and why is the compiler complaining?
int array[X];then using plainarrayis the same as&array[0], with the typeint *; And 2) For any array or pointerpand indexi, the expressionp[i]is exactly equal to*(p + i), which means that all "array" indexing is really pointer arithmetic. These two things is what sometimes can make arrays and pointers seem similar.int **arrayandint array[m][n]may or may not implement your preferred abstraction to some degree or another. But they are different, incompatible things as far as C is concerned.for (int i = 0; i < i; i++) {- When do you expectito be less thani? Will this loop ever run? Given thatiappears before the loop, it looks like theilocally scoped to your list is shadowing theiin the outer scope.