copy array - row-wise

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Soumyadip Rakshit

    #1

    copy array - row-wise

    I have a 2D Array of Integers A[3][5]. I would like to copy it to another
    array B[3][5] taking each row at a time. They are both initialized as
    pointers to pointers.

    I would like to use something like the following but it doesn't seem to work

    for (i=0; i < 3; i++) {
    while(*A[i]++ = *B[i]++)
    ;
    }


  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In what way does it not work? Unless one of the pointers in each row of B is NULL it will never end because there is no other end condition on the while loop.

    Additionally because of operator precedence

    *A[i]++ elavulates as *(A[i]++) which is not what you want, you will be writing to and from the wrong locations.

    Is there a reason you don't want to memcpy?

    memcpy( A, B, sizeof(A) );

    or use 2 indices

    for (i=0; i < 3; i++) {
    for (j=0; j < 5; j++) {
    A[i][j] = B[i][j];
    }
    }

    Comment

    • Grumble

      #3
      Re: copy array - row-wise

      Soumyadip Rakshit wrote:[color=blue]
      > I have a 2D Array of Integers A[3][5]. I would like to copy it to
      > another array B[3][5] taking each row at a time. They are both
      > initialized as pointers to pointers.
      >
      > I would like to use something like the following but it doesn't seem
      > to work
      >
      > for (i=0; i < 3; i++) {
      > while(*A[i]++ = *B[i]++)
      > ;
      > }[/color]

      Could you provide the definition of A and B?

      Comment

      • Michael Mair

        #4
        Re: copy array - row-wise

        Soumyadip Rakshit schrieb:[color=blue]
        > I have a 2D Array of Integers A[3][5]. I would like to copy it to another
        > array B[3][5] taking each row at a time. They are both initialized as
        > pointers to pointers.
        >
        > I would like to use something like the following but it doesn't seem to work
        >
        > for (i=0; i < 3; i++) {
        > while(*A[i]++ = *B[i]++)
        > ;
        > }[/color]

        Please provide a compiling minimal example or your best shot
        at it. Your declaration of A and B might be wrong or your
        way of testing or ...

        Cheers
        Michael
        --
        E-Mail: Mine is an /at/ gmx /dot/ de address.

        Comment

        • stathis gotsis

          #5
          Re: copy array - row-wise

          "Soumyadip Rakshit" <soumyadipraksh it@gmx.net> wrote in message
          news:Iuqoqo.Mx6 @bath.ac.uk...[color=blue]
          > I have a 2D Array of Integers A[3][5]. I would like to copy it to another
          > array B[3][5] taking each row at a time. They are both initialized as
          > pointers to pointers.
          >
          > I would like to use something like the following but it doesn't seem to[/color]
          work[color=blue]
          >
          > for (i=0; i < 3; i++) {
          > while(*A[i]++ = *B[i]++)
          > ;
          > }[/color]

          If you have allocated space for array B and want to copy the integer values
          stored in array A to array B, i would recommend you go with the simple:

          for (i=0;i<2;i++)
          for (j=0;j<3;j++)
          B[i][j]=A[i][j];


          Comment

          • Keith Thompson

            #6
            Re: copy array - row-wise

            "Soumyadip Rakshit" <soumyadipraksh it@gmx.net> writes:[color=blue]
            > I have a 2D Array of Integers A[3][5]. I would like to copy it to another
            > array B[3][5] taking each row at a time. They are both initialized as
            > pointers to pointers.
            >
            > I would like to use something like the following but it doesn't seem to work
            >
            > for (i=0; i < 3; i++) {
            > while(*A[i]++ = *B[i]++)
            > ;
            > }[/color]

            If they're initialized as pointers to pointers, then you don't have a
            2D array. Show us the declarations and initialization code.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Barry Schwarz

              #7
              Re: copy array - row-wise

              On Wed, 15 Feb 2006 17:32:48 GMT, "Soumyadip Rakshit"
              <soumyadipraksh it@gmx.net> wrote:
              [color=blue]
              >I have a 2D Array of Integers A[3][5]. I would like to copy it to another
              >array B[3][5] taking each row at a time. They are both initialized as
              >pointers to pointers.[/color]

              Arrays are arrays and pointers are pointers. The fact that they use
              the same [] syntax for accessing objects pointed to is both a source
              of flexibility for the language and a source of confusion for
              newcomers.

              If your assertion about the definition is correct, then A and B each
              point to 3 pointers to integer each. These pointers in turn point to
              5 integers each (you did not say int). If you want to copy the 5
              integers pointed to by A[0] to the memory pointed to by B[0], you can
              use memcpy (or memmove but that is overkill unless the areas overlap)
              or a loop.
              [color=blue]
              >
              >I would like to use something like the following but it doesn't seem to work
              >
              >for (i=0; i < 3; i++) {
              > while(*A[i]++ = *B[i]++)
              > ;[/color]

              There are several problems with this while loop:

              You have the assignment backwards. This attempts to copy B to
              A, not A to B as your specified.

              After the first iteration, A[0] no longer points to the first
              of the five integers. Ditto for B[0]. When you exit the while loop,
              if it were to execute only five times, A[0] would end up pointing to
              the byte just beyond the integer known as A[0][4]. Ditto for B[0].

              What makes you think that B[i][4] will always be zero. Strings
              are arrays of char that are nul ('\0') terminated. Arrays of integer
              in general are under no such constraint. If a zero occurs before the
              fifth element, you will not copy the entire array. If none of the
              five is zero, you invoke undefined behavior by stepping beyond the
              bounds of the five integers A[0] and B[0] point to.
              [color=blue]
              >}
              >[/color]


              Remove del for email

              Comment

              Working...