ggoyo wrote:It is actually true, an array is a pointer but the only difference is that it is constant...
No, an array is not a pointer, constant or otherwise.
http://eli.thegreenplace.net/2009/10/21 ... lent-in-c/ gives more technical explanation with reference to some produced assembly.
ggoyo wrote:You cannot pass it to a function as you pointed it out (no pun intended), what would happen if the function changes the adress to the first element ? If the function argument is (const **int x) then you can pass a 2D array to it, but cannot use pointer arithmetic inside that function.
Firstly you
can pass a 1D array to a function even without a const specifier but only because the compiler
converts the array to a pointer, and it does this whether you declare the parameter as a pointer or as an array. If an array were already a pointer, constant or otherwise, then no conversion could happen. Additionally since the array is converted into a pointer then pointer arithmetic works within the function. The parameter is passed by value so no changes to the pointer within the function can affect the address of the external array. If you try the following code:
Code: Select all
#include <stdio.h>
int firstElement(int x[])
{
++x;
return x[0];
}
int main()
{
int x[3]= {1,2,3};
printf("First element of the array = %d\n",firstElement(x));
printf("First element of the array = %d\n",x[0]);
return 0;
}
you'll see that it compiles without warning and prints "First element of the array = 2" followed by "First element of the array = 1", demonstrating the above points. The code works identically if you declare "int firstElement(int* x)".
But what if we try something similar with a 2D array? If we use "int firstElement(int x[][]) the compiler bails out even if we also use the 'const'. If we try your 'const' solution and no pointer arithmetic...
Code: Select all
/* Arrays are not pointers */
#include <stdio.h>
int firstElement(const int** x)
{
return x[0][0];
}
int main()
{
int x[2][3]= {
{1,2,3},
{4,5,6}
};
printf("First element of the array = %d\n",firstElement(x));
return 0;
}
This compiles, but with the following warnings under gcc:
Code: Select all
warning: passing argument 1 of 'firstElement' from incompatible pointer type
note: expected 'const int **' but argument is of type 'int (*)[3]'
Because a 2D array is an array of arrays, it tries to convert it to a pointer to arrays but the function asks for a pointer to a pointer and so the compiler flags a warning.
If I try and run the program it fails on my machine with an access violation, indicating 'x' within the function hasn't been initialised to point to the beginning of the array.
You can't consider a 2D array as a pointer to pointers - constant or otherwise - because it's an array of arrays, and pointers and arrays are different. If you still have trouble understanding, consider that multidimensional arrays know their shape, yet how would an int** (whose value is purely an address) know where to find its element (1,1) in memory?