Elementary C++, assigning arrays

In summary, it is not possible to assign the contents of array2 to array1 with the following statement.
  • #1
Lord Anoobis
131
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
  • #2
That didn't quite post the way I expected...
 
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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."
 
  • #8
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.
 

1. What is an array in C++?

An array in C++ is a data structure that allows you to store a collection of elements of the same data type in consecutive memory locations. It can be accessed by using a numerical index to specify the position of the element you want to access.

2. How do I declare an array in C++?

To declare an array in C++, you need to specify the data type of the elements in the array, followed by the name of the array and the number of elements it can hold enclosed in square brackets. For example: int myArray[5]; This declares an array of integers named "myArray" that can hold 5 elements.

3. How do I assign values to an array in C++?

You can assign values to an array in C++ by using a loop to iterate through the elements of the array and assign values to each element. Alternatively, you can also use the array index notation to directly assign values to specific elements in the array. For example: myArray[0] = 1; assigns the value 1 to the first element in the array.

4. Can I change the size of an array in C++?

No, the size of an array in C++ is fixed and cannot be changed once it has been declared. If you need to store more elements, you will need to declare a new array with a larger size and copy the elements from the existing array into the new one.

5. How do I access elements in a multidimensional array in C++?

In a multidimensional array, elements are accessed using multiple indices. The first index specifies the row, and the second index specifies the column. For example: myArray[2][3] would access the element in the third row and fourth column of the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
755
  • Engineering and Comp Sci Homework Help
Replies
8
Views
842
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
Back
Top