|
|
|
|
|
|
|
In this hour, you learned how to create arrays in C++. An array is a fixed-size collection of objects that are all the same type. |
|
|
|
|
|
|
|
|
Arrays don't do bounds checking. Therefore, it is legaleven if disastrousto read or write past the end of an array. Arrays count from 0. A common mistake is to write to off-set n of an array of n members. |
|
|
|
|
|
|
|
|
Arrays can be one-dimensional or multidimensional. In either case, the members of the array can be initialized, as long as the array contains either built-in types, such as int, or objects of a class that has a default constructor. |
|
|
|
|
|
|
|
|
Arrays and their contents can be on the free store or on the stack. If you delete an array on the free store, remember to use the brackets in the call to delete. |
|
|
|
|
|
|
|
|
Array names are constant pointers to the first elements of the array. Pointers and arrays use pointer arithmetic to find the next element of an array. |
|
|
|
|
|
|
|
|
Strings are arrays of characters, or chars. C++ provides special features for managing char arrays, including the capability to initialize them with quoted strings. |
|
|
|
|
|
|
|
|
Q What happens if I write to element 25 in a 24-member array? |
|
|
|
|
|
|
|
|
A You will write to other memory, with potentially disastrous effects on your program. |
|
|
|
|
|
|
|
|
Q What is in an uninitialized array element? |
|
|
|
|
|
|
|
|
A Whatever happens to be in memory at a given time. The results of using this member without assigning a value are unpredictable. |
|
|
|
|
|
|
|
|
A Yes. With simple arrays you can use pointers to combine them into a new, larger array. With strings you can use some of the built-in functions, such as strcat, to combine strings. |
|
|
|
|
|