row-wise copying of 2D arrays

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++)
;
}
 
V

Victor Bazarov

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++)


Which direction are you copying, again? How long are you copying?

I don't understand the need to increment the pointers themselves, neither
do I understand your use of 'while' instead of a 'for(5)':

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

This should be relatively straight-forward. What's the need to dance
around with those ++ operations? Are you trying to shave off some CPU
ticks? Why?

V
 
D

default

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++)
;
}

After the loop is complete, wouldn't A and B point to the _end_ of the
array, not the beginning? So do make sure to backup the original pointers
before beginning the loop. Also, are you one-hundred per cent sure the
array is zero-terminated? (like for c-strings)
For all I know, the code you posted should theoretically work. Perhaps a
more complete example would be nice?
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top