Printing 2D Arrays with Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Super KHB

    #1

    Printing 2D Arrays with Pointers

    I know that I am close, but it wont work. HELP!!!!

    void PrintA( int * , int);

    void main()
    {

    int num[3][5] = {
    {1, 2,3,4,5},
    {6,7,8,9,10},
    {11,12,13,14,15 },
    };
    int *num_ptr;
    num_ptr = &num[0][0];

    PrintA( num_ptr, 5 );
    }

    void PrintA( int *a, int cols )
    {
    for (int r = 0; r < 3 ; r++)
    {for ( int c = 0; c < 5; c++ )
    cout << *(*(a + r) + c) << " ";
    cout << endl;
    }
    }
  • Siemel Naran

    #2
    Re: Printing 2D Arrays with Pointers

    "Super KHB" <look@message.e nd> wrote in message news:Jx0gd.4908 3
    [color=blue]
    > void PrintA( int *a, int cols )
    > {
    > for (int r = 0; r < 3 ; r++)
    > {for ( int c = 0; c < 5; c++ )
    > cout << *(*(a + r) + c) << " ";
    > cout << endl;
    > }
    > }[/color]

    Should the line
    [color=blue]
    > cout << *(*(a + r) + c) << " ";[/color]

    be

    cout << *(*(a + 5*r) + c) << " ";


    Also, instead of the hardcoded constant "5", do you mean to use the variable
    "cols"?


    Comment

    • Karl Heinz Buchegger

      #3
      Re: Printing 2D Arrays with Pointers

      Super KHB wrote:[color=blue]
      >
      > I know that I am close, but it wont work. HELP!!!!
      >
      > void PrintA( int * , int);
      >
      > void main()
      > {
      >
      > int num[3][5] = {
      > {1, 2,3,4,5},
      > {6,7,8,9,10},
      > {11,12,13,14,15 },
      > };[/color]

      Here you have a memory layout looking like this:

      num
      +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
      | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
      +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

      and the compiler keeps track of calulating the correct offset from the beginning
      when given 2 indices
      [color=blue]
      > int *num_ptr;
      > num_ptr = &num[0][0];
      >
      > PrintA( num_ptr, 5 );
      > }
      >
      > void PrintA( int *a, int cols )
      > {
      > for (int r = 0; r < 3 ; r++)
      > {for ( int c = 0; c < 5; c++ )
      > cout << *(*(a + r) + c) << " ";[/color]

      Here you assume a memory layout like this:

      num
      +------+ +----+----+----+----+----+
      | o--------->| 1 | 2 | 3 | 4 | 5 |
      | | +----+----+----+----+----+
      +------+ +----+----+----+----+----+
      | o------------------>| 6 | 7 | 8 | 9 | 10 |
      | | +----+----+----+----+----+
      +------+ +----+----+----+----+----+
      | o------------>| 11 | 12 | 13 | 14 | 15 |
      | | +----+----+----+----+----+
      +------+

      ( an array of pointers, where each pointer points to an array of int )

      Totally different.



      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • Venki

        #4
        Re: Printing 2D Arrays with Pointers

        > cout << *(*(a + r) + c) << " ";

        try this cout << *(a+((cols * r) + c))<< " ";

        Comment

        • Old Wolf

          #5
          Re: Printing 2D Arrays with Pointers

          "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote:[color=blue]
          > "Super KHB" <look@message.e nd> wrote:
          >[color=green]
          > > void PrintA( int *a, int cols )
          > > {
          > > for (int r = 0; r < 3 ; r++)
          > > {for ( int c = 0; c < 5; c++ )
          > > cout << *(*(a + r) + c) << " ";
          > > cout << endl;
          > > }
          > > }[/color]
          >
          > Should the line[color=green]
          > > cout << *(*(a + r) + c) << " ";[/color]
          > be
          > cout << *(*(a + 5*r) + c) << " ";[/color]

          We are dealing with a pointer to int so there should be
          exactly one indirection. I would write it as:
          a[r * cols + c]

          (NB. Actually I wouldn't write this because I think
          it is UB. However we have just had a big discussion
          with no conclusion on that topic in another thread
          so I don't want to start it up again here).

          Comment

          Working...