Comp Sci Elementary C++, assigning arrays

  • Thread starter Thread starter Lord Anoobis
  • Start date Start date
  • Tags Tags
    Arrays Elementary
AI Thread Summary
Assigning one array to another directly in C++ using the statement `array1 = array2;` is not possible because array names act as pointers and cannot be assigned as modifiable l-values. Instead, each element must be assigned individually using a loop. Attempts to access elements beyond the declared size of an array, such as `array1[5]`, lead to undefined behavior due to out-of-bounds access. The discussion also highlights that using functions like `memcpy` can facilitate copying arrays without explicit loops. Understanding these concepts is crucial for proper array manipulation in C++.
Lord Anoobis
Messages
131
Reaction score
22

Homework Statement


Assuming that
Code:
array1
and
Code:
array2
are both arrays, why is it not possible to assign
the contents of
Code:
array2
to
Code:
array1
with the following statement?

Code:
array1 = array2;

Homework Equations

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:
#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:
#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:
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:
Physics news on Phys.org
That didn't quite post the way I expected...
 
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?
 
Lord Anoobis said:
Code:
array1 = array2;
As far as I can tell the given line doesn't work because the variables still need to be specified as arrays with [ ].
You seem to be missing an important point: arrays variables are pointers.
Lord Anoobis said:
Code:
  int array1[5], array2[] = {1, 2, 3, 4, 5};
  array1[5] = array2[5];
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?

Lord Anoobis said:
Code:
  int array1[] = {array2[5]};
The presence of the curly brackets here doesn't do what you think it does.

Lord Anoobis said:
Why is this happening and how does one go about correctly setting one array equal to another? Without using a loop, that is.
Look up memcpy.
 
DrClaude said:
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?
I see it. There IS no sixth element. I'm not sure how that one slipped by.
 
phyzguy said:
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?
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.
 
Lord Anoobis said:
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.
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."
 
Mark44 said:
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."
I'll be sure to remember that. Thanks.
 

Similar threads

Replies
3
Views
1K
Replies
8
Views
1K
Replies
2
Views
3K
Replies
3
Views
2K
Replies
7
Views
2K
Replies
15
Views
2K
Back
Top