Below is an example of two dimensional array.
int s[5][2] = {
{0, 1},
{2, 3},
{4, 5},
{6, 7},
{8, 9}
};
int (*p)[2];
If I write p = &s[0]; there is no error. But if I write p = s[0]; there is an error, even though &s[0] and s[0] will give the same address.
Please let me know why there is a differnece, even though both give the same address.
The addresses are the same, but the types are different.
&s[0] is of type int (*)[2], but s[0] is of type int [2], decaying to int *.
The result is that when performing arithmetic on p, the pattern with which it walks over the array will depend on its type. If you write p = &s[0] and then access p[3][1], you are accessing s[3][1]. If on the other hand you write int *q = s[0], you can only use q to access the first subarray of s e.g. q[1] will access s[0][1].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With