shifting data from one array to another array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: shifting data from one array to another array

  1. #1
    Join Date
    Dec 2008
    Posts
    23

    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........

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  3. #3
    Join Date
    Dec 2008
    Posts
    23
    erm sry for the typo.

    it should be: E2start[1]=E1start[1];

    so does E2start[1] have the same data as E1start[1]?

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    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

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    23
    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 11:55 PM.

  7. #7
    Join Date
    Dec 2007
    Posts
    401
    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' ;
    }

  8. #8
    Join Date
    Dec 2008
    Posts
    23
    okok ty very much i will try out if still cant get it i will post it up

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    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

  1. Replies: 1
    Last Post: 04-10-2009, 03:16 AM
  2. Replies: 7
    Last Post: 03-07-2006, 05:34 PM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 11: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center