c:basic_arrays
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
c:basic_arrays [2016/08/05 14:56] – peter | c:basic_arrays [2021/04/28 08:49] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== C - Basic Arrays ====== | ====== C - Basic Arrays ====== | ||
- | **NOTE**: | + | <WRAP info> |
- | Array basics | + | **NOTE**: |
+ | </ | ||
- | Let's start by looking at a single variable used to store a person' | + | ---- |
- | C Code Listing 1 | + | ===== A single variable is used to store the age of a person ===== |
+ | |||
+ | ==== C Code Listing 1 ==== | ||
<code c> | <code c> | ||
Line 19: | Line 22: | ||
9: } | 9: } | ||
</ | </ | ||
+ | |||
+ | ---- | ||
| | ||
- | C++ Code Listing 1 | + | ==== C++ Code Listing 1 ==== |
<code cpp> | <code cpp> | ||
Line 34: | Line 39: | ||
</ | </ | ||
| | ||
- | Not much to it. The variable age is created at line (5) as a short. | + | <WRAP info> |
+ | **NOTE: | ||
+ | |||
+ | * A value is assigned to age. | ||
+ | * Finally, age is printed to the screen. | ||
+ | |||
+ | </ | ||
Image: A simple variable in memory | Image: A simple variable in memory | ||
Line 40: | Line 51: | ||
{{: | {{: | ||
+ | ---- | ||
+ | |||
+ | ===== Keep track of 4 ages instead of just one ===== | ||
- | Now let's keep track of 4 ages instead of just one. We could create | + | 4 separate variables |
- | Rather than using 4 separate variables, we'll use an array. | + | |
- | Here's how to create an array and one way to initialize | + | Instead of using separate variables |
- | C Code Listing 2 | + | ==== C Code Listing 2 ==== |
<code c> | <code c> | ||
Line 62: | Line 75: | ||
</ | </ | ||
- | C++ Code Listing 2 | + | ---- |
- | <code c++> | + | ==== C++ Code Listing 2 ==== |
+ | |||
+ | <code cpp> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 78: | Line 93: | ||
</ | </ | ||
- | On line (5), an array of 4 short' | + | <WRAP info> |
+ | **NOTE: | ||
+ | |||
+ | * On line (5), an array of 4 shorts | ||
+ | * Values are assigned to each variable in the array on line (6) through line (9). | ||
+ | |||
+ | </ | ||
Image: Array with 4 elements | Image: Array with 4 elements | ||
- | {{: | + | {{: |
+ | |||
+ | ---- | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | Simply provide a number in square braces next to the name of the array. | ||
- | Accessing any single short variable, or element, in the array is straightforward. | + | The program above shows that the first element |
- | The program above shows that the first element of an array is accessed with the number 0 rather than 1. Later in the tutorial, We'll discuss why 0 is used to indicate the first element in the array. | + | </ |
+ | ---- | ||
===== Printing arrays ===== | ===== Printing arrays ===== | ||
- | Our program is a bit unrevealing in that we never print the array to screen. | + | Print the array to the screen: |
- | C Code Listing 3 | + | ==== C Code Listing 3 ==== |
<code c> | <code c> | ||
Line 111: | Line 140: | ||
</ | </ | ||
- | C++ Code Listing 3 | + | ---- |
+ | |||
+ | ==== C++ Code Listing 3 ==== | ||
<code c++> | <code c++> | ||
Line 129: | Line 160: | ||
</ | </ | ||
- | Line (11) is meant to print the 4 ages to the screen. | + | <WRAP info> |
+ | **NOTE: | ||
+ | |||
+ | But instead of printing out the four short variables, what appears to be nonsense prints out instead. | ||
+ | |||
+ | What the " | ||
+ | |||
+ | For now, the important point to come away with is that simply providing the name of the array in an output statement will not print out the elements of the array. | ||
- | What the " | + | </ |
+ | ---- | ||
How about printing out each of the values separately? | How about printing out each of the values separately? | ||
- | C Code Listing 4 | + | ==== C Code Listing 4 ==== |
<code c> | <code c> | ||
Line 156: | Line 195: | ||
</ | </ | ||
- | C++ Code Listing 4 | + | ---- |
+ | |||
+ | ==== C++ Code Listing 4 ==== | ||
<code c++> | <code c++> | ||
Line 176: | Line 217: | ||
</ | </ | ||
- | Lines (10) through line (13) produce the output we are expecting. | + | <WRAP info> |
+ | **NOTE: | ||
- | There is no single statement in the language that says "print an entire array to the screen" | + | There is no single statement in the language that says "print an entire array to the screen" |
+ | Each element in the array must be printed to the screen individually. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
===== Copying arrays ===== | ===== Copying arrays ===== | ||
Line 185: | Line 232: | ||
Suppose that after filling our 4 element array with values, we need to copy that array to another array of 4 short' | Suppose that after filling our 4 element array with values, we need to copy that array to another array of 4 short' | ||
- | C Code Listing 5 | + | ==== C Code Listing 5 ==== |
<code c> | <code c> | ||
Line 209: | Line 256: | ||
</ | </ | ||
- | C++ Code Listing 5 | + | ---- |
+ | |||
+ | ==== C++ Code Listing 5 ==== | ||
<code c++> | <code c++> | ||
Line 233: | Line 282: | ||
</ | </ | ||
- | Line (12) tries to copy the age array into the same_age array. | + | <WRAP info> |
+ | **NOTE: | ||
The point here is that simply assigning one array to another will not copy the elements of the array. | The point here is that simply assigning one array to another will not copy the elements of the array. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
Let's try copying arrays using a technique similar to the technique used to print arrays (that is, one element at a time): | Let's try copying arrays using a technique similar to the technique used to print arrays (that is, one element at a time): | ||
- | C Code Listing 6 | + | ==== C Code Listing 6 ==== |
<code c> | <code c> | ||
Line 267: | Line 321: | ||
</ | </ | ||
- | C++ Code Listing 6 | + | ---- |
+ | |||
+ | ==== C++ Code Listing 6 ==== | ||
<code c++> | <code c++> | ||
Line 295: | Line 351: | ||
</ | </ | ||
- | This technique for copying arrays works fine. Two arrays are created: age and same_age. | + | <WRAP info> |
+ | **NOTE** | ||
+ | |||
+ | * Two arrays are created: age and same_age. | ||
+ | * Each element of the age array is assigned a value. | ||
+ | * Then, in order to copy of the four elements in age into the same_age array, we must do it element by element. | ||
+ | |||
+ | </ | ||
Image: Copy element 1 | Image: Copy element 1 | ||
+ | |||
Copy first element | Copy first element | ||
- | {{: | + | {{: |
Image: Copy element 2 | Image: Copy element 2 | ||
+ | |||
Copy second element | Copy second element | ||
Line 314: | Line 379: | ||
One significant advantage of an array over separate variables is the name. In our examples, using four separate variables requires 4 unique names. The 4 short variables in our array have the same name, age. The 4 short' | One significant advantage of an array over separate variables is the name. In our examples, using four separate variables requires 4 unique names. The 4 short variables in our array have the same name, age. The 4 short' | ||
- | C Code Listing 7 | + | ==== C Code Listing 7 ==== |
<code c> | <code c> | ||
Line 338: | Line 403: | ||
</ | </ | ||
- | C++ Code Listing 7 | + | ---- |
+ | |||
+ | ==== C++ Code Listing 7 ==== | ||
<code c++> | <code c++> | ||
Line 368: | Line 435: | ||
If we want to perform any action on an array, we must repeatedly perform that action on each element in the array. | If we want to perform any action on an array, we must repeatedly perform that action on each element in the array. | ||
+ | ---- | ||
===== Array Internals ===== | ===== Array Internals ===== | ||
Line 402: | Line 470: | ||
Image: Variable fl allocated | Image: Variable fl allocated | ||
- | {{: | + | {{: |
When fl is used in line (5), two distinct steps occur: | When fl is used in line (5), two distinct steps occur: | ||
Line 456: | Line 524: | ||
Image: Array with 4 elements | Image: Array with 4 elements | ||
- | {{: | + | {{: |
* On line (11), The program finds and grabs the address reserved for age. Simple enough. | * On line (11), The program finds and grabs the address reserved for age. Simple enough. | ||
Line 514: | Line 582: | ||
Image: Array with 4 elements | Image: Array with 4 elements | ||
- | {{: | + | {{: |
Did you answer " | Did you answer " | ||
Line 597: | Line 665: | ||
Image: int array | Image: int array | ||
- | {{: | + | {{: |
In line (4), miles is the address of an integer, or an int*. By the rules of pointer arithmetic, line (4) will get the contents at address 928. | In line (4), miles is the address of an integer, or an int*. By the rules of pointer arithmetic, line (4) will get the contents at address 928. | ||
Line 612: | Line 680: | ||
Image: float array | Image: float array | ||
- | {{: | + | {{: |
In line (4), gpa is the address of a floating point number, or a float*. | In line (4), gpa is the address of a floating point number, or a float*. | ||
Line 627: | Line 695: | ||
Image: char array | Image: char array | ||
- | {{: | + | {{: |
In line (4), letter is the address of an character, or a char*. | In line (4), letter is the address of an character, or a char*. |
c/basic_arrays.1470408968.txt.gz · Last modified: 2020/07/15 09:30 (external edit)