- An array is simply a collection of more than one of the same type of variable. For example, a list of high scores is a type of array, as would be how many miles you run every day, even if that list contains lots of zeros. And, of course, strings in the C language are an array of single-character variables. In this movie, I introduce the concept of the array. You'll see how to create an array, how to fill it with data, how to access that data, and I'll also divulge some secrets about character arrays, also known as strings.
Here you see the way not to do multiple values in your code. Four separate float variables are created and filled with data. They're then displayed using four separate printf statements. Each variable requires its own repetitive statement. You can build and run this code, but why bother? Instead, I'll show you a better way to deal with multiple variables of the same type. That way is the array. Open exercise array2.
This is the array version of what you saw in the previous exercise. The array temps is created at line five. It contains four elements, and each element is assigned in the brackets that follow. The loop at line nine displays all four elements' values by using a single printf function. An array is simply a collection of multiple variables all of the same type. It has a declaration similar to any other variable, but with square brackets after the variable name.
The brackets hold the number of elements in the array, or they can be blank if the elements are specified when the array is declared. Here, the integer array deliveries has room for 15 elements. Integer array totals has three elements, which are assigned in curly brackets. The final element does not have a comma. You put a comma there, the compiler believes that you've forgotten something. You can also list array elements on a line by themselves. Don't forget the commas, except after the final element.
And remember to close the curly bracket and add a semicolon. Each element in the array is its own variable. You specify the element between the square brackets. The first element is element zero. You can also use an integer variable to specify array elements. Here, variable n represents a specific element in the totals array. The element is used like any other variable. It can find itself inside a printf function, or it can be used with an assignment operator, as shown here.
Finally, you cannot change the number of elements in an array after the array has been declared. Some tricks exist to work around this limitation, but for now, just accept it as a rule. Also, don't forget that the first element in an array is zero, not one. Humans start counting at one. The C language starts counting at zero. An integer array calories is declared at line seven. It has meals element, and the meals constant is set to three at line three.
A for loop at line 12 reads in three values. See how the for loop starts at zero? That comes in handy when working with arrays, as the first element is zero. In line 14, however, one is added to the looping variable x, which makes the numbers more accommodating to humans running the program. The scanf statement at line 15 reads the values into each array element. An array element is an individual variable, so the ampersand is required.
At line 16, the value input is added to the total variable. Build and run the code. I'm going to type some easy numbers so I can confirm the computer's math. Yep, it works. You'll see the variable total is initialized at line 10. That's required. Otherwise, the variable may contain garbage. In C, variables are not initialized until they're assigned a value. Comment out line 10 to prove this.
Save, build and run to see if it has any effect. And, of course, the number is off because the total variable was not properly initialized. There's an off chance that it may be correct, because a random number could be zero, in case, 600 would show up. But here, you see that the total variable was somehow containing the value 52. Strings are character arrays. Normally, they're declared by using double quotes.
Open exercise array4. The character array text is declared at line five. This is really simple, and it's one of the best ways to declare a string or character array. Open exercise array5. This code works the same as the previous exercise, but the character array is declared character by character, which is a phenomenal waste of time. Build and run the code. It works, but it's just too much effort. The code does, however, point out that the final character in all C language strings, which must be specified, is the null character.
It's shown here as the escape sequence \0. Now, when you use double quotes to declare a string, the compiler automatically adds the null for you. But when you specify one character at a time, you must remember to add the null. The null comes in handy when you display a string one character at a time. Open array6. This code displays the string in the hello array one character at a time. The while loop marches through the string until the null character is encountered.
It spits out single characters by incrementing variable n as it progresses. Build and run this code. Because the null character evaluates to the false condition in the C language, you can shorten the while decision to this. Make that change in your code, save, build and run. The same output is generated, although the source code is a tad less readable to a beginning programer.
- Understanding a C program
- Adding comments
- Using escape characters
- Working with values and placeholders
- Introducing variables
- Making a decision with if
- Adding functions
- Manipulating strings
- Building arrays
- Nesting structures