copy array - row-wise

S

Soumyadip Rakshit

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++ = *B++)
;
}
 
G

Grumble

Soumyadip said:
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++ = *B++)
;
}


Could you provide the definition of A and B?
 
M

Michael Mair

Soumyadip said:
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++ = *B++)
;
}


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
 
S

stathis gotsis

Soumyadip Rakshit said:
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++ = *B++)
;
}


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[j]=A[j];
 
K

Keith Thompson

Soumyadip Rakshit said:
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++ = *B++)
;
}


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

Barry Schwarz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top