Computer Science 1037A/B Lecture 1: Arrays
Document Summary
Array size is (often) fixed at the time of declaration. Array is a data structure that represents a collection of the same types of data. C++ requires that the array size used to declare an array must be a constant expression. Right int size = 4; double mylist[size]; const int size = 4; double mylist[size]; The array elements are accessed through the index. After an array is created, an indexed variable can be used in the same way as a regular variable. Accessing array elements using subscripts beyond the boundary does not cause syntax errors, but the operating system might report a memory access violation. Declaring, creating, initializing in one step: datatype arrayname[arraysize] = {value0, value1,, valuek}; Example: double mylist[4] = {1. 9, 2. 9, 3. 4, 3. 5}; Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Wrong double mylist[4]; mylist = {1. 9, 2. 9, 3. 4, 3. 5};