C++ Question (Arrays and memory)
|dw:1484092193468:dw|
@Ultrilliam I found the answer. I thought it would be nice to share. In C++ when we say int array[5], we are giving the array the size of 5, but from 0 to 4 only. So, 0, 1, 2, 3, 4, which are five of them. Anything beyond that is considered not in the scope of the array size and will probably return garbage value. Also, if you declare an array , int array[5], but you Do not give each array values such as array[0] = 2; array[1] = 11; . . . array[4] = 100; Then the program does not know what exists at that memory when you print it out like this. for (int i = 0; i < 5; i++){ cout << array << endl; } Output: Random garbage values (Ex. 97423625) which you never intended to use.
A slight correction: for (int i = 0; i < 5; i++){ cout << array * << endl; }
The answer does not place an i inside [ ] in the front of array, but you get my idea.
Join our real-time social learning platform and learn together with your friends!