Why size of array declaration is not require? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why size of array declaration is not require?

Is it possible without size of array declaration we can use it. It may occur like memory leak problem. int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl; //Outputs 1652

2nd Apr 2019, 3:44 AM
Amar Singh
Amar Singh - avatar
2 Answers
+ 3
Array can be of any size, that's not the problem. For loop might be the issue if you loop it more times than there are elements. You will need to determine the length of the array somehow dynamically. Check out the std::array and range for, for example. https://stackoverflow.com/questions/20234898/correct-way-of-loop-through-the-c-arrays
2nd Apr 2019, 4:04 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
0
I didn't understand it, can u elaborate your answer please.
3rd Apr 2019, 7:50 AM
Amar Singh
Amar Singh - avatar