MAT 21B Lecture 14: Lecture 14
Lecture 14
See powerpoint for code lines
Dynamic size
- The size of the initialization list determines the number of elements
Indexing
- Any expression that produces an integer can be used as subscript
- As long as subscript stays within bounds (see powerpoint for code line)
Loops
- Very often arrays are meant to be sequentially accessed
o For loop is the perfect repetition structure
o Use of a constant for the array size is always recommended
Bounds
- The behavior of out of bound access is difficult to predict
- Segmentation fault errors always come from accessing a memory location that
doesn’t contain a valid object
Pointer constant
- Each element has its own memory address
- Addresses are 4byte apart
- Value associated to the array name is an address
o It’s a pointer
o But cannot be reassigned to another address: pointer constant
o Indexes are distances from this pointer
- A pointer variable can be used like an array
o Pointer itself points on the first element
o Index gives the distance from first element
find more resources at oneclass.com
find more resources at oneclass.com
beigecamel74 and 41 others unlocked
83
MAT 21B Full Course Notes
Verified Note
83 documents
Document Summary
The size of the initialization list determines the number of elements. Any expression that produces an integer can be used as subscript. As long as subscript stays within bounds (see powerpoint for code line) Very often arrays are meant to be sequentially accessed: for loop is the perfect repetition structure, use of a constant for the array size is always recommended. The behavior of out of bound access is difficult to predict. Segmentation fault errors always come from accessing a memory location that doesn"t contain a valid object. Each element has its own memory address. Value associated to the array name is an address: it"s a pointer, but cannot be reassigned to another address: pointer constant, indexes are distances from this pointer. A pointer variable can be used like an array: pointer itself points on the first element, index gives the distance from first element.