473,785 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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]++)
;
}
Feb 15 '06 #1
6 3216
Banfa
9,065 Recognized Expert Moderator Expert
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];
}
}
Feb 15 '06 #2
Soumyadip Rakshit wrote:
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]++)
;
}


Could you provide the definition of A and B?
Feb 15 '06 #3
Soumyadip Rakshit schrieb:
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]++)
;
}


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.
Feb 15 '06 #4
"Soumyadip Rakshit" <so************ **@gmx.net> wrote in message
news:Iu******** @bath.ac.uk...
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]++)
;
}


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];
Feb 15 '06 #5
"Soumyadip Rakshit" <so************ **@gmx.net> writes:
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]++)
;
}


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) ks***@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.
Feb 15 '06 #6
On Wed, 15 Feb 2006 17:32:48 GMT, "Soumyadip Rakshit"
<so************ **@gmx.net> wrote:
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[i]++ = *B[i]++)
;
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.
}

Remove del for email
Feb 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4802
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i<nrow;i++)
4
8826
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
1
1553
by: dwight | last post by:
I load an excel spreadsheet into an object array, . What is the best way to copy a row into a single array or arraylist? Currently I just loop through each column and assign it to the new array. xlData was poplulated from excel interop tosData = new object ; for (int curCol = 1; curCol < 109; curCol++) tosData = xlData;
2
4397
by: frankh | last post by:
With Marshal you can copy data from a one-dimensional array to BitmapData.Scan0. What do I do if my array is two-dimensional? Or, alternatively, can I create somehow two variables byte arr1; // size 480*640 byte arr2; // size which are references to the same memory block and allow me to access it at will with one or two indices, e.g. arr1 or arr2?
15
8337
by: Sam | last post by:
Hi, I have the following code : Dim rNodeList As New ArrayList Dim rNodeListNew As New ArrayList For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1 rNodeList.Add(tvRelations.Nodes(iCntr).Text) Next
3
21797
by: Silvester | last post by:
My datagridview has columns with text AND integers. Please can someone help me with the code necessary to copy an entire selected row of mixed content cells to an array ? Thanks very much
0
1344
by: fmlamela | last post by:
Hi, I have to create a bi-dimensional matrix, where each row is a int array of a specific size. For adding a new row, I use the ArrayList.Add (int ) command. I would like now to convert this ArrayList object, to a int variable. I only managed to do it with a double "for loop" for reading each row and column. Considering we have N rows and M columns, I have tried first trying to copy each single row: int arr = new int;...
2
4017
by: barker7 | last post by:
I use a simple double array plus a variable to store the row size to represent two dimensional data. I need to quickly copy this data to a two dimensional array: double. Currently I iterate through the double, one by one and place it into the preallocated double. The arrays are large, and I'm doing a lot of these. Thus it has become a significant bottleneck. Can anyone suggest a faster way to convert a double + row size info to a...
5
3623
by: VijaKhara | last post by:
Hi all, I have a 2-D array with the size M x N. Now I need to segment this array into two parts with sizes P x N and (M-P) x N. P | M-P |--------|------------------------| | | | | | | | | | N
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.