A simple program is:
Code:
//Array Access
#include<stdio.h>
void main()
{
int n[3][3]={
2,4,3,
6,8,5,
3,5,1
};
int i,*ptr;
ptr=&n;
for(i=0;i<9;i++)
{
printf("%d",*(ptr+i)); //should print 2,6 and 3 as n[0],n[1] and n[2]
}
}
Now my understanding is a 2 dimensional array is nothing but a collection of one dimensional array within another one dimensional array.So when i am pointing n,the address is held in ptr like n[0][0]..
and *(ptr+i)=ptr[i]=i[ptr]
So ptr[i] should=ptr[0],ptr[1],etc which is invalid as it is not pointing to any particular element like ptr[0][1] or ptr[1][2].So how the output is printed?
Help me in correcting the concept!!
I also know we can access each element of such array if we have already initialised a pointer to an array.But here nothig is done like that!!