1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Elementary C++, assigning arrays

  1. Oct 21, 2015 #1
    1. The problem statement, all variables and given/known data
    Assuming that
    Code (Text):
    array1
    and
    Code (Text):
    array2
    are both arrays, why is it not possible to assign
    the contents of
    Code (Text):
    array2
    to
    Code (Text):
    array1
    with the following statement?

    Code (Text):
    array1 = array2;

    2. Relevant equations


    3. The attempt at a solution
    This is a question I just saw in a book. As far as I can tell the given line doesn't work because the variables still need to be specified as arrays with [ ]. Anyway, I carried out a little experiment to see what happens when one tries something like this and I found...

    Code (Text):

    #include <iostream>

    int main ()
    {
      int array1[5], array2[] = {1, 2, 3, 4, 5};
      array1[5] = array2[5];

      for (int i = 0; i < 5; i++)
      std::cout << array2[i] << "\t";
      std::cout << std::endl;

      for (int j = 0; j < 5; j++)
      std::cout << array1[j] << "\t";
      std::cout << std::endl;

      return 0;

    }
    Appears to assign random values to array1. Then I tried this...

    Code (Text):

    #include <iostream>

    int main ()
    {
      int array2[] = {1, 2, 3, 4, 5};
      int array1[] = {array2[5]};

      for (int i = 0; i < 5; i++)
      std::cout << array2[i] << "\t";
      std::cout << std::endl;

      for (int j = 0; j < 5; j++)
      std::cout << array1[j] << "\t";
      std::cout << std::endl;

      return 0;

    }
    Which surprisingly yields a vastly improved (but still not quite correct) result. Even more bizarre is the result obtained when in the second program in line 6 we have
    Code (Text):
    int array1[5] = {array2[5]}
    . Why is this happening and how does one go about correctly setting one array equal to another? Without using a loop, that is.
     
    Last edited: Oct 21, 2015
  2. jcsd
  3. Oct 21, 2015 #2
    That didn't quite post the way I expected...
     
  4. Oct 21, 2015 #3

    phyzguy

    User Avatar
    Science Advisor

    I don't think it is possible without looping through the index and assigning array1[j] = array2[j] for each j. What's wrong with doing this?
     
  5. Oct 21, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    You seem to be missing an important point: arrays variables are pointers.


    There is a big difference between declaring an array as array1[5], i.e., having a size of 5, as in the first line above, to referencing the sixth element array1[5], as in the second line. The second statement above will have unpredictable consequences. Do you see why?

    The presence of the curly brackets here doesn't do what you think it does.

    Look up memcpy.
     
  6. Oct 21, 2015 #5
    I see it. There IS no sixth element. I'm not sure how that one slipped by.
     
  7. Oct 21, 2015 #6
    That's how I've been doing it, but I wanted to see what would happen if I tried it in the way I described.
     
  8. Oct 21, 2015 #7

    Mark44

    Staff: Mentor

    What happens is a consequence of the difference in C between array1 (an address) and array1[k] (the element of the array at index k). The variable array1 is read-only, so it cannot appear on the left side of an assignment statement. In C parlance, "array1 is not a modifiable l-value."
     
  9. Oct 22, 2015 #8
    I'll be sure to remember that. Thanks.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Elementary C++, assigning arrays
Loading...