-
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?😒0
-
Member • Sep 9, 2010
Suppose you have declared a array A[4][6],xxxabhash007I want to know how the 2-D arrays are stored in the memory, exactly how does the compiler look at the 2-D array?😒
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.Are you sure? This action cannot be undone. -
Member • Sep 9, 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.Are you sure? This action cannot be undone. -
Member • Sep 9, 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.Are you sure? This action cannot be undone. -
Member • Sep 9, 2010
What is the output?
show(int(*q)[4],int row,int col)
{
int i,j,*p;
for(i=0;i<row;i++)
{
p=q+i;
for(j=0;j<col;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=4Are you sure? This action cannot be undone. -
Member • Sep 11, 2010
Can anyone give me the output of the program which I have posted.Are you sure? This action cannot be undone. -
Member • 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 meAre you sure? This action cannot be undone. -
Member • Sep 11, 2010
Can you please give the actual output in double quotes.Are you sure? This action cannot be undone. -
Member • Sep 11, 2010
It will give you error
ie you cannot convert int *[4] to int *Are you sure? This action cannot be undone. -
Member • Sep 12, 2010
can you please explain the logic behind itgoyal420It will give you error
ie you cannot convert int *[4] to int *Are you sure? This action cannot be undone. -
Member • Sep 13, 2010
@Goyal: There is no such kind of statement "int *[4]" or "int *". Then what are you talking about?Are you sure? This action cannot be undone. -
Member • Sep 14, 2010
show(int(*q)[4],int row,int col) { int i,j,*p; for(i=0;i<row;i++) { p=q+i; for(j=0;j<col;j++) printf("%d",*(p+j)); printf("\n"); } printf("\n"); }
Here 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 outputAre you sure? This action cannot be undone.