Arrays
8 min · C
Fixed-size arrays — hold many values under one name.
Enemy 1 of 5
Declaring and indexing an array
An array groups several values of the same type together under one name, stored contiguously in memory (right next to each other). You declare one with type name[size]; — for example int nums[3]; makes room for exactly three ints — and you can provide initial values right away with curly braces, like int nums[3] = {3, 1, 2};.
Elements are accessed with square brackets and a zero-based index: nums[0] is the first element, nums[1] is the second, nums[2] is the third (and last, in a 3-element array). Reading or writing nums[3] on this array would reach one slot past the end — C does not stop you from doing this, and it's one of the most notorious sources of bugs and security vulnerabilities in real C programs, so always double-check your bounds.
Loading editor...