C++20 sections · 1024 units
Open in Course

Accessing Elements

Reading and writing values

Access elements with []. For int nums[5];, nums[0] is the first element, nums[1] is the second. You can read or write this way. To read: int x = nums[2]; copies element to x.

To write: nums[2] = 42; stores 42. Array elements work like regular variables. Indexing is fast. The compiler calculates base_address + (index * element_size). Accessing nums[0] and nums[1000] takes the same time.