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?😒

Replies

  • Whats In Name
    Whats In Name
    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
    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
    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
    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
    Can anyone give me the output of the program which I have posted.
  • anandkumarjha
    anandkumarjha
    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
    Can you please give the actual output in double quotes.
  • Manish Goyal
    Manish Goyal
    It will give you error

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

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

Related Posts

I HAVE MADE THIS PAGE AND HOPING FOR A GOOD RESPONSE, SPECIALLY FROM THE YOUTH. I WANT ALL OF YOU TO BE A PART OF THIS INITIATIVE AGAINST CORRUPTION AND...
What exactly a "End task" feature do(Task Manager) which a "Close" Feature can't? Why doesn't simply closing a non-responding document works?😕
Ferrari engineers have developed F10 simulator that lets your experience the joy of driving an F10 virtually and also compete with others on the Fiorano circuit. The simulator is available...
BAE's Military Air Solutions (MAS) will cut about 700 jobs over the next few days. At the company’s Farnborough facility, 55 jobs could be slashed because of the government’s 2009...
What is thread?? How blocking of threads can be possible????