View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12940 Members
Join this group to post and comment.
xxxabhash007
xxxabhash007 • Sep 10, 2010

Accessing arrays

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?πŸ˜’
Whats In Name
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
xxxabhash007 • Sep 10, 2010
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
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
xxxabhash007 • Sep 10, 2010
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
xxxabhash007 • Sep 11, 2010
Can anyone give me the output of the program which I have posted.
anandkumarjha
anandkumarjha • Sep 11, 2010
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
xxxabhash007 • Sep 11, 2010
Can you please give the actual output in double quotes.
It will give you error

ie you cannot convert int *[4] to int *
anandkumarjha
anandkumarjha • Sep 12, 2010
goyal420
It will give you error

ie you cannot convert int *[4] to int *
can you please explain the logic behind it
xxxabhash007
xxxabhash007 • Sep 13, 2010
@Goyal: There is no such kind of statement "int *[4]" or "int *". Then what are you talking about?
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

Share this content on your social channels -