CrazyEngineers
  • Accessing arrays

    Updated: Oct 25, 2024
    Views: 1.0K
    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
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Whats In Name

    MemberSep 9, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • xxxabhash007

    MemberSep 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.
    Cancel
  • Whats In Name

    MemberSep 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.
    Cancel
  • xxxabhash007

    MemberSep 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=4
    Are you sure? This action cannot be undone.
    Cancel
  • xxxabhash007

    MemberSep 11, 2010

    Can anyone give me the output of the program which I have posted.
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 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
    Are you sure? This action cannot be undone.
    Cancel
  • xxxabhash007

    MemberSep 11, 2010

    Can you please give the actual output in double quotes.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberSep 11, 2010

    It will give you error

    ie you cannot convert int *[4] to int *
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 12, 2010

    goyal420
    It will give you error

    ie you cannot convert int *[4] to int *
    can you please explain the logic behind it
    Are you sure? This action cannot be undone.
    Cancel
  • xxxabhash007

    MemberSep 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.
    Cancel
  • Manish Goyal

    MemberSep 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 output
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register