Accessing memory address of 2d array using pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mVosa
    New Member
    • Aug 2006
    • 1

    #1

    Accessing memory address of 2d array using pointers

    I have this piece of code in a funtion which is trying to retrieve the memory address of values that are stored in a 2D array, using pointers..[B].Could I get help or tips on how to do this please :)...I'm new at this programming..

    void displayADD(floa t temp[][DAY])
    {
    int m,d;
    float *ptr;
    ptr=&temp[0][0];

    *ptr=temp[m][d];}

    }}
    cout<<"The address storing this value is "<<&ptr<<en dl;

    }

    Vinaka.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    That is not a complete code chunk (1 { 4 } make it hard to follow)

    however

    ptr=&temp[0][0];

    Sets ptr to point to the very first entry in the arry

    *ptr=temp[m][d];

    Copys the value at m, d to where ever ptr is currently pointing to, in this example temp[0][0]

    cout<<"The address storing this value is "<<&ptr<<en dl;

    Prints the address of ptr this is not the memory location of temp[0][0] but the memory location of ptr itself.

    If you want to print the memory location of temp[0][0] you need

    cout<<"The address storing this value is "<<ptr<<end l;

    Comment

    • shinelakshmanan
      New Member
      • Aug 2006
      • 13

      #3
      ptr=&temp[0][0],
      after execution of this instruction ptr will be loaded with the address of 1st row 1st column of array temp,
      matrix elements are stored row wise in C,
      so suppose temp is a 5 by 6 matrix and you want to get the address of
      temp[4][5],
      simply increase individual subscripts by 1,multiply them and subtract 1 from the product,
      so 4 is increased to 5,
      5 is increased to 6,
      their product is equal to 30,
      subtract 1 from it,
      which leads to 29,
      so address of temp[4][5] could be obtained by
      (ptr+29)
      and its value by
      *(ptr+29)

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by shinelakshmanan
        ptr=&temp[0][0],
        after execution of this instruction ptr will be loaded with the address of 1st row 1st column of array temp,
        matrix elements are stored row wise in C,
        so suppose temp is a 5 by 6 matrix and you want to get the address of
        temp[4][5],
        simply increase individual subscripts by 1,multiply them and subtract 1 from the product,
        so 4 is increased to 5,
        5 is increased to 6,
        their product is equal to 30,
        subtract 1 from it,
        which leads to 29,
        so address of temp[4][5] could be obtained by
        (ptr+29)
        and its value by
        *(ptr+29)
        That unfortunately is wrong. You have the right sort of principle but have your maths wrong.

        This is correct
        Code:
        int array[5][6];
        int *ptr = &array[0][0];
        
        // Then array[4][5] can be accessed as
        
        *(ptr+ (4*6)+5)
        in the code above
        *(ptr+ (4*6)+5)

        written descriptively is

        *(ptr + (the number of the first index to access * the declared size of the second index) + the number of the second index)

        4 * 6 + 5 = 29

        This is the same as your result but that is only by chance of the numbers used

        Look at the array entry

        array[4][4]

        By your method it is at (4+1)*(4+1)-1 = 24

        but it is actually at (4*6)+4 = 28

        Note that you can also see that array[4][4] should be 1 place behind array[4][5] and by my calculation it is (29 - 28 = 1).

        Comment

        • shinelakshmanan
          New Member
          • Aug 2006
          • 13

          #5
          sorry you are correct....

          Comment

          Working...