From the course: Complete Guide to C Programming Foundations
Understanding arrays - C Tutorial
From the course: Complete Guide to C Programming Foundations
Understanding arrays
- [Instructor] In this code, you see five float variables, each of which holds a highscore value and is named appropriately, but isn't this type of declaration inefficient? There must be a better way to represent these similar values. In C, an array is a collection of variables, all of the same data type. That's the short definition. Specifically, an array is a variable. Arrays are declared like any variable. They have a data type and a name, but the name is followed by square brackets. A value within the brackets specifies the number of items or elements within the array. Here's an array variable declaration. The data type is float, the name is highscore, and the array contains five elements, five float values, that are collectively referenced in this array. This is a better way to store five highscore values as shown earlier. Like any variable, you can initialize an array when it's declared. To do so, follow the declaration with an assignment operator, equals, and a set of braces to hold the assigned values. Each element's value is separated by a comma, though the final value lacks a trailing comma. And when you assign values in this manner, you can omit the number of elements from the declaration. The compiler figures out the value on its own. Individual array elements are accessed in this format. The value in brackets represents the element number. The value can be expressed as a literal or a constant, and it can be a variable or any valid expression. But most importantly, you must remember that the first element of an array is always zero. The highscore array has five elements. Its elements are numbered 0, 1, 2, 3, and 4. Therefore, variable highscore 2 is the third element of the array. This numbering scheme is something all programmers forget occasionally, even myself. Also, remember that arrays in C are non-dynamic. This limitation means that you cannot alter the number of elements in an array when the program runs. The alternative is to use a pointer to allocate storage, which is an advanced concept covered elsewhere in this course. Though you set the size of an array, the compiler doesn't provide any bounds-checking. In other words, it's possible to reference array elements that don't exist. This causes various problems in a program, none of which are good. It's important that your code track an array's size to ensure that bad things don't happen.
Contents
-
-
-
-
-
-
-
-
Understanding arrays2m 48s
-
(Locked)
Working with arrays2m 51s
-
(Locked)
Challenge: Creating an array55s
-
(Locked)
Solution: Creating an array1m 41s
-
(Locked)
Passing an array to a function2m 45s
-
(Locked)
Working with multi-dimensional arrays2m 42s
-
(Locked)
Building a structure3m 31s
-
(Locked)
Nesting structures3m 17s
-
(Locked)
Challenge: Constructing a structure1m 3s
-
(Locked)
Solution: Constructing a structure3m 5s
-
(Locked)
Putting structures into an array2m 37s
-
(Locked)
Working with structures and functions3m 47s
-
(Locked)
Chapter challenge: Manipulating a structure1m 22s
-
(Locked)
Chapter solution: Manipulating a structure2m 4s
-
-
-
-
-
-