-
shifting data from one array to another array
guys, i have a question to ask:
E2start[1]=E2start[1];
does E2start[1] have the same data as E1start[1]?
seriously need help........
-
Your code does not reference E1 at all. what you have is e2 = e2.
So, from your code, e2 may or may not be the same as e1.
-
erm sry for the typo.
it should be: E2start[1]=E1start[1];
so does E2start[1] have the same data as E1start[1]?
-
Yes, assuming that E2start and E1start are one dimensional arrays and that they both contain elements of the same type, e.g. they're both int arrays, Date arrays etc.
To answer with certainty you need to say what the datatype of these arrays is.
Danny Kalev
-
yea, its actually possible to make a user defined type where the = operation is not exactly what you might think. It could, actually, write "I am the king of the compiler" 8000 times instead of actually copying any data... some programmers are like that.
-
oic, but is there any way whereby i can change the data of E1start[1] without touching the data in the E2start[1]? That means E2start[1] will have the previous data of E1start[1] and now E1start[1] will have the new data.
they are both int arrays.
Last edited by abc456; 05-12-2009 at 10:55 PM.
-
yes. each element of an array of int holds a copy of an int.
to get answers to questions of this kind, write a small program and see what it does. and play around with the program by modifying it in several ways and checking out what the behaviour is.
Code:
#include <iostream>
int main()
{
enum { N = 5 } ;
int array_one[N] = { 0, 1, 2, 3, 4 } ;
int array_two[N] = { 99, 99, 99, 99, 99 } ;
std::cout << "array_one[0] == " << array_one[0] << '\n' ;
std::cout << "array_two[0] == " << array_two[0] << '\n' ;
array_one[0] = array_two[0] ;
std::cout << "after array_one[0] = array_two[0] ;\n" ;
std::cout << "array_one[0] == " << array_one[0] << '\n' ;
std::cout << "array_two[0] == " << array_two[0] << '\n' ;
array_two[0] = -76543 ;
std::cout << "after array_two[0] = -76543 ;\n" ;
std::cout << "array_one[0] == " << array_one[0] << '\n' ;
std::cout << "array_two[0] == " << array_two[0] << '\n' ;
}
-
okok ty very much i will try out if still cant get it i will post it up
-
data copying is, if anything, done to excess. If you do x = y, the computer copys the data but x and y are in different memory locations, they just happen (well, by intent) to contain the same bytes for now. If you then do x = 3, y does not change.
If you *want* a change in x to also change y, you have to do this:
int &x = y;
now, x and y are in the same memory location and a change to either x or y will change both.
Pointers also can be confusing. A function:
foo(int x) {x = 5;}
y = 3;
foo(y); ///after this y is STILL 3!! if you want to change y in this manner, use:
foo(int &x) /////the rest is the same, but now y will change to 5.
Finally, you cannot pass copys of arrays or pointers:
foo (int * array) or foo (int array[]) //these are identical, and I may have a typo
....
array[3] = 11;
....
foo( some_array); ////some_array will be changed! The only way to really stop this behavior is to copy it yourself before passing it, OR put a const in the function to enforce not changing it (compiler error, you simply cannot change it).
Similar Threads
-
By JavaEnthusiast in forum C++
Replies: 1
Last Post: 04-10-2009, 02:16 AM
-
By Java_Noob in forum Java
Replies: 7
Last Post: 03-07-2006, 04:34 PM
-
By Tim Frost in forum xml.announcements
Replies: 0
Last Post: 04-02-2001, 10:53 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks