Mulitdimensional Arrays and Pointers
Hello to all.
I can't understand why this snippet
Code:
#include<stdio.h>
int main(void)
{
int arr2d[2][4] = { { 0 , 1 , 2 , 3 } , {4, 5 , 6 , 7}} , (*p)[4] , i=0;
for( p = &arr2d[0]; p < &arr2d[2]; p++)
{
printf(" %d %d " , (*p)[i] , arr2d[0][0] );
}
return 0;
}
Will advance the pointer Number_of_columns * sizeof(int) .
Instead of this code
Code:
#include<stdio.h>
int main(void)
{
int arr2d[2][4] = { { 0 , 1 , 2 , 3 } , {4, 5 , 6 , 7}} , *p ;
for( p = arr2d[0]; p < arr2d[1] + 4 ; p++)
{
printf("%d " , *p );
}
return 0;
}
Which will advance pointer (p) 4 bytes (assuming in my system that int has 4 bytes size ) . The first snippet will process an column but the second a row. I know that... but I can't understand why in the second situation will advance the pointer with this size . (#COLUMNS * sizeof(int) ) . Due to & in front of arr2d[0] ?
Yes but if we have p = &arr2d[0] ... arr2d[0] is a pointer , hence &arr2d[0] is a pointer to pointer but p is declared as a signle pointer without some object by the compiler. arr2d[0] is a pointer to arr2d[0][0] . Why this ?
(*p)[4] is a double pointer declaration??? (cheating) .