Elementary C++, assigning arrays

  • Context: Comp Sci 
  • Thread starter Thread starter Lord Anoobis
  • Start date Start date
  • Tags Tags
    Arrays Elementary
Click For Summary

Discussion Overview

The discussion revolves around the assignment of arrays in C++, specifically addressing why a direct assignment like array1 = array2; is not valid. Participants explore the implications of array declarations, pointer behavior, and the need for looping to copy array contents.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants assert that direct assignment of arrays is not possible due to the need for specifying array sizes and the nature of array variables as pointers.
  • One participant describes an experiment with code that leads to unexpected results, indicating confusion about array indexing and initialization.
  • Another participant emphasizes that array variables are read-only and cannot be assigned directly, highlighting the distinction between an array's address and its elements.
  • Some participants suggest that copying arrays must be done through looping, while others inquire about alternatives like using memcpy.
  • There is acknowledgment of the consequences of accessing out-of-bounds elements, with one participant realizing the mistake of referencing a non-existent sixth element.

Areas of Agreement / Disagreement

Participants generally agree that direct assignment of arrays is not valid, but there is no consensus on the best method to copy arrays without using loops. Multiple views on the nature of arrays and pointers are presented.

Contextual Notes

Participants discuss the implications of array size, pointer behavior, and the consequences of accessing out-of-bounds elements, but do not resolve the best practices for array assignment.

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 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K