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.
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.
I would like to use something like the following but it doesn't seem to work
for (i=0; i < 3; i++) {
while(*A++ = *B++)
;
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[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.
Remove del for email