Ask your own question, for FREE!
Computer Science 17 Online
korosh23:

C++ Question (Arrays and memory)

korosh23:

|dw:1484092193468:dw|

korosh23:

@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.

korosh23:

A slight correction: for (int i = 0; i < 5; i++){ cout << array * << endl; }

korosh23:

The answer does not place an i inside [ ] in the front of array, but you get my idea.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!