Mark44
Mentor
- 38,061
- 10,576
No, moreNumbers is an array of 25 ints, and myNumbers is an array of 5 ints. Arrays in C and C++ have indexes ranging from 0 to (number of elements - 1).yungman said:I copied the file I input according to the example in the book. The question only want to read the values in both arrays. That's NOT the question I want to ask. My question is about constexpr that making the moreNumber(24) , a 24 element array.
You're not creating constexpr on an array -- you're just calculating a number and using it to declare an array of that size. If myNumbers array is defined to have N elements, the moreNumbers array will have ##N^2## elements, This keyword is a fairly new addition to C++, having been added to the C++ 11 standard, and revised a bit for the C++ 14 standard.yungman said:The book does not talk anything or the syntax on how to create constexpr on array, I just very interest in learning. I tried google, all the example have too many terms I have not learned, so they don't make sense to me. Because of potential error if I write and read back to say moreNumbers(20). If the array is defined wrong, this would be out of bound, but the compiler would not flag out of bound( according to the book). So even if I read back the correct value, that doesn't verify the moreNumber() is created correctly.
Regarding your program, keep in mind that the myNumbers array has only 5 elements (indexed 0 through 4), so if you enter an index larger than 4, you will be storing a number in the myNumbers array well beyond its last element, potentially overwriting something else.
ARRAY_LENGTH is a constant -- 5, so line 10 calculates the value Square(ARRAY_LENGTH) as 25 at compile time. So the moreNumbers array is declared as an array of type int with 25 elements.yungman said:If you look at line 3 and line 10, Something really looks wrong to me. line 3 defines
line3: constexpr int Square(int number) { return number * number; }
line10: int moreNumbers[Square(ARRAY_LENGTH)];
How is line 3 set up for line 10? Can anyone explain this to me?