1. Home >
2. Apps >
3. Groups >

Accessing arrays

Question asked by xxxabhash007 in #Coffee Room on Sep 10, 2010
xxxabhash007 路 Sep 10, 2010
Rank D1 - MASTER
I want to know how the 2-D arrays are stored in the memory, exactly how does the compiler look at the 2-D array?😒 Posted in: #Coffee Room
Whats In Name 路 Sep 10, 2010
xxxabhash007
I want to know how the 2-D arrays are stored in the memory, exactly how does the compiler look at the 2-D array?馃槖
Suppose you have declared a array A[4][6],
Then in memory there will be 6 continuous blocks of 4 continuous rows allocated to this.

[-][-][-][-][-]
[-][-][-][-][-][-]
[-][-][-][-][-][-]
[-][-][-][-][-][-]

So if you want to access S,then the address will be A[1][5].

-Correct me if I am wrong.
xxxabhash007 路 Sep 10, 2010
Rank D1 - MASTER
But I have read in Yashwant's Pointers In C:In memory there are no rows and columns. In memory whether it's a 1-D or 2-D array the elements are stored in one continuous chain.
Whats In Name 路 Sep 10, 2010
Memory is in continuous chain,but the way to access it is different.
It can be possible that rows are in continuous form,Eg :
Continuous Memory:
[-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-]
Can be viewed as:
Row 1:[-][-][-][-]Row 2:[-][-][-][-]Row 3:[-][-][-][-]Row 4:[-][-][-][-]So on..
Within computer.
xxxabhash007 路 Sep 10, 2010
Rank D1 - MASTER
What is the output?
show(int(*q)[4],int row,int col)
{
int i,j,*p;
for(i=0;i {
p=q+i;
for(j=0;j printf("%d",*(p+j));
printf("\n");
}
printf("\n");
}
where argument *q[4] is receiving the array:
a[3][4]={ {1,2,3,4},
{5,6,7,8},
{9,0,1,6}
}
row=3 & col=4
xxxabhash007 路 Sep 11, 2010
Rank D1 - MASTER
Can anyone give me the output of the program which I have posted.
anandkumarjha 路 Sep 11, 2010
Rank C2 - EXPERT
as far as i know the output will be the address of the 3rd row and 4the column element i.e the address of the element 6....if i am wrong then plsss correct me
xxxabhash007 路 Sep 11, 2010
Rank D1 - MASTER
Can you please give the actual output in double quotes.
Manish Goyal 路 Sep 12, 2010
Rank A2 - PRO
It will give you error

ie you cannot convert int *[4] to int *
anandkumarjha 路 Sep 12, 2010
Rank C2 - EXPERT
goyal420
It will give you error

ie you cannot convert int *[4] to int *
can you please explain the logic behind it
xxxabhash007 路 Sep 13, 2010
Rank D1 - MASTER
@Goyal: There is no such kind of statement "int *[4]" or "int *". Then what are you talking about?
Manish Goyal 路 Sep 14, 2010
Rank A2 - PRO
```show(int(*q)[4],int row,int col)
{
int i,j,*p;
for(i=0;iHere p=q+i is invalid statement

Since p is a pointer to array
where as q is pointer to array of columns

you cannot directly store address of q in p

in that case you have to modify this statement as follow
p=&q[0];

with this modification the  code will work and will print the array q as its output
```

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up