c:basic_arrays
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c:basic_arrays [2016/08/05 14:21] – created peter | c:basic_arrays [2021/04/28 08:49] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== C - Basic Arrays ====== | ====== C - Basic Arrays ====== | ||
- | **NOTE**: | + | <WRAP info> |
+ | **NOTE**: | ||
+ | </ | ||
- | Array basics | + | ---- |
- | Let's start by looking at a single variable used to store a person's age. | + | ===== A single variable |
- | C Code Listing 1 | + | ==== C Code Listing 1 ==== |
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 17: | Line 21: | ||
8: | 8: | ||
9: } | 9: } | ||
- | C++ Code Listing 1 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 1 ==== | ||
+ | |||
+ | <code cpp> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 27: | Line 37: | ||
8: | 8: | ||
9: } | 9: } | ||
- | Not much to it. The variable age is created at line (5) as a short. A value is assigned to age. Finally, age is printed to the screen. | + | </ |
+ | |||
+ | <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 | ||
- | Now let's keep track of 4 ages instead of just one. We could create | + | {{: |
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Keep track of 4 ages instead of just one ===== | ||
+ | |||
+ | 4 separate variables | ||
+ | |||
+ | Instead | ||
- | Here's how to create an array and one way to initialize an array: | + | ==== C Code Listing 2 ==== |
- | C Code Listing 2 | + | <code c> |
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 47: | Line 73: | ||
| | ||
11: } | 11: } | ||
- | C++ Code Listing 2 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 2 ==== | ||
+ | |||
+ | <code cpp> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 59: | Line 91: | ||
| | ||
11: } | 11: } | ||
- | 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 | ||
- | Accessing any single short variable, or element, in the array is straightforward. Simply provide a number in square braces next to the name of the array. The number identifies which of the 4 elements in the array you want to access. | + | {{: |
+ | |||
+ | ---- | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | Simply provide a number in square braces next to the name of the array. | ||
+ | |||
+ | The program above shows that the first element of an array is accessed with the number 0 rather than 1. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Printing arrays ===== | ||
- | 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 | + | Print the array to the screen: |
- | Printing arrays | + | |
- | Our program is a bit unrevealing in that we never print the array to screen. Here is the same program with an attempt to print the array to the screen: | + | ==== C Code Listing 3 ==== |
- | C Code Listing 3 | + | <code c> |
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 84: | Line 138: | ||
| | ||
13: } | 13: } | ||
- | C++ Code Listing 3 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 3 ==== | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 98: | Line 158: | ||
| | ||
13: } | 13: } | ||
- | Line (11) is meant to print the 4 ages to the screen. But instead of printing out the four short variables, what appears to be nonsense prints out instead. | + | </ |
- | What the " | + | <WRAP info> |
- | How about printing out each of the values separately? Try this: | + | **NOTE: |
- | C Code Listing 4 | + | 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. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | How about printing out each of the values separately? | ||
+ | |||
+ | ==== C Code Listing 4 ==== | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 119: | Line 193: | ||
| | ||
15: } | 15: } | ||
- | C++ Code Listing 4 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 4 ==== | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 135: | Line 215: | ||
| | ||
15: } | 15: } | ||
- | 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" | ||
+ | |||
+ | Each element in the array must be printed to the screen individually. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
- | There is no single statement in the language that says "print an entire array to the screen" | + | ===== Copying arrays ===== |
- | Copying arrays | + | 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> |
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 163: | Line 254: | ||
| | ||
19: } | 19: } | ||
- | C++ Code Listing 5 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 5 ==== | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 183: | Line 280: | ||
| | ||
19: } | 19: } | ||
- | Line (12) tries to copy the age array into the same_age array. What happened when you tried to compile the program above? | + | </ |
+ | |||
+ | <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. The hard question to answer is why the code doesn' | ||
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> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 212: | Line 319: | ||
| | ||
23: } | 23: } | ||
- | C++ Code Listing 6 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 6 ==== | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 236: | Line 349: | ||
| | ||
23: } | 23: } | ||
- | This technique for copying arrays works fine. 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. | + | </ |
+ | |||
+ | <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 | ||
- | Like printing arrays, there is no single statement in the language that says "copy an entire array into another array" | + | {{: |
- | The technique used to copy one array into another | + | Like printing arrays, there is no single statement in the language that says "copy an entire |
- | One significant advantage of an array over separate variables | + | The technique used to copy one array into another |
- | C Code Listing 7 | + | 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 ==== | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 270: | Line 401: | ||
| | ||
19: } | 19: } | ||
- | C++ Code Listing 7 | + | </ |
+ | |||
+ | ---- | ||
+ | |||
+ | ==== C++ Code Listing 7 ==== | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 290: | Line 427: | ||
| | ||
19: } | 19: } | ||
- | Since the only difference between each of the short' | + | </code> |
- | Even though arrays give us some convenience when managing many variables of the same type, there is little difference between an array and variables declared individually. There is no single statement to copy an array, there is no single statement to print an array. | + | Since the only difference between each of the short' |
+ | |||
+ | Even though arrays give us some convenience when managing many variables of the same type, there is little difference between an array and variables declared individually. | ||
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 | ||
To understand what exactly is happening inside of arrays, first recall the behavior of variables from the pointer tutorial: | To understand what exactly is happening inside of arrays, first recall the behavior of variables from the pointer tutorial: | ||
Try: | Try: | ||
- | C++ C | + | |
+ | ^C++^C^ | ||
+ | | | ||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: int main() | 2: int main() | ||
Line 309: | Line 453: | ||
| | ||
7: } | 7: } | ||
+ | </ | ||
+ | | | ||
+ | <code c> | ||
| | ||
2:int main() | 2:int main() | ||
Line 316: | Line 463: | ||
| | ||
7: } | 7: } | ||
- | At line (4) in the program above, the computer reserves memory for fl. In our examples, we'll assume that a float requires 4 bytes. Depending on the computer' | + | </ |
+ | | | ||
+ | |||
+ | At line (4) in the program above, the computer reserves memory for fl. In our examples, we'll assume that a float requires 4 bytes. | ||
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: | ||
- | The program finds and grabs the address reserved for fl--in this example 924. | + | * The program finds and grabs the address reserved for fl--in this example 924. |
- | The contents stored at that address are retrieved | + | |
To generalize, whenever any variable is accessed, the above two distinct steps occur to retrieve the contents of the variable. | To generalize, whenever any variable is accessed, the above two distinct steps occur to retrieve the contents of the variable. | ||
- | So, how is this related to referring to arrays without providing an index number (e.g. age, not age[2])? | + | So, how is this related to referring to arrays without providing an index number (e.g. age, not age[2])? |
Here's the program from earlier in the tutorial that was our first crude attempt at printing out the contents of an array: | Here's the program from earlier in the tutorial that was our first crude attempt at printing out the contents of an array: | ||
C Code Listing 8 | C Code Listing 8 | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 343: | Line 498: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
C++ Code Listing 8 | C++ Code Listing 8 | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 357: | Line 516: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
Line (11) prints out nonsense, not the contents of our array. | Line (11) prints out nonsense, not the contents of our array. | ||
Line 363: | 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. According to the image, age starts at address 924 so it is address 924 that is grabbed. | + | {{: |
- | The contents stored at that address are retrieved (Oops!). | + | |
- | Step 2 is the problem. Our array has four values, not just one. To retrieve the contents at 924 would be inappropriate because 924 is the address of the first value in the array, not all values in the array. | + | |
- | As a result, step 2 isn't performed on arrays, just step 1. The output from line (11) is the address | + | * On line (11), The program finds and grabs the address |
+ | * The contents | ||
- | Accessing elements | + | Step 2 is the problem. |
- | From the pointer | + | As a result, step 2 isn't performed on arrays, just step 1. The output from line (11) is the address of the array: step 2 has not occurred. |
+ | |||
+ | |||
+ | ===== Accessing elements in an array ===== | ||
+ | |||
+ | From the [[c: | ||
C Code Listing 9 | C Code Listing 9 | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 387: | Line 554: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
C++ Code Listing 9 | C++ Code Listing 9 | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 401: | Line 572: | ||
| | ||
13: } | 13: } | ||
- | On line (11) the * operator has been added. When this code is compiled and executed, 23 prints to the screen. When just the array' | + | </code> |
- | That's all fine and good, but what about the rest of the array? What if we want to access the second, third, or fourth element of our array? We know that when we use the name of an array, step 1 occurs and we get the address of the first element of that array. We also know (from the pointers tutorial) that an address is nothing more than an integer. Given that the address is nothing but an integer, we can use addition to access the other array elements! | + | On line (11) the * operator has been added. When this code is compiled and executed, 23 prints to the screen. |
+ | |||
+ | That's all fine and good, but what about the rest of the array? What if we want to access the second, third, or fourth element of our array? | ||
The big question is: How much do we add to age in line (11) above to get the address of the second element in the age array? | The big question is: How much do we add to age in line (11) above to get the address of the second element in the age array? | ||
Line 409: | Line 582: | ||
Image: Array with 4 elements | Image: Array with 4 elements | ||
- | Did you answer " | + | {{: |
- | In C and C++ you add " | + | Did you answer " |
+ | |||
+ | In C and C++ you add " | ||
Here is how we can print the value of the second element in the array: | Here is how we can print the value of the second element in the array: | ||
C Code Listing 10 | C Code Listing 10 | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 429: | Line 606: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
C++ Code Listing 10 | C++ Code Listing 10 | ||
+ | |||
+ | <code c++> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 443: | Line 624: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
Line (11) now shows pointer arithmetic in action: | Line (11) now shows pointer arithmetic in action: | ||
- | Step 1 is done on age--the address 924 is returned. | + | * Step 1 is done on age--the address 924 is returned. |
- | Using pointer arithmetic, 1 is added to 924 which produces the address of the next element in the array. Looking at the image of our example, the result is 926. | + | |
- | Using the * operator ("do step 2 on a number" | + | |
The parentheses are required to ensure that the * operator and the + operator occur in the correct order. This is due to operator precedence rules. | The parentheses are required to ensure that the * operator and the + operator occur in the correct order. This is due to operator precedence rules. | ||
- | Pointer Arithmetic | ||
- | "This new learning amazes me, Sir Bedevere! Explain again, how sheep' | + | ===== Pointer Arithmetic ===== |
+ | |||
+ | "This new learning amazes me, Sir Bedevere! | ||
-King Arthur, Monty Python and the Holy Grail | -King Arthur, Monty Python and the Holy Grail | ||
- | Why is pointer arithmetic different from normal arithmetic? If you recall from the pointers tutorial, pointers in C and C++ are typed. That is, in C and C++ you can have float pointers, int pointers, char pointers, etc. Here's an excerpt from a side note in the pointers tutorial: | ||
- | ...A primary motivation for having a pointer type for each variable type is to help the compiler. Referring back to an earlier example, when we get the contents at an address ("do step 2 on a number" | + | Why is pointer arithmetic different from normal arithmetic? |
+ | |||
+ | | ||
The compiler needs to know two things when getting the contents at an address: | The compiler needs to know two things when getting the contents at an address: | ||
- | How many bytes starting at the given address make up the value. For example, does the address refer to a 2 byte short int or a 4 byte float or an 8 byte double? | + | * How many bytes starting at the given address make up the value. |
- | How to convert those bytes into the value. That is, the process for converting 4 bytes into a float is different than the process for converting 2 bytes into a short int. | + | |
- | We've seen that when the name of an array is specified all by itself, only step 1 is done, not step 2. After step 1, we have the address to the first element in the array. In other words, it is a pointer! What kind of pointer? The type of the pointer is the same type as the array. In our sample program we created an array of short integers. So, the address is a short pointer (that is, it is a: short*). | + | |
+ | We've seen that when the name of an array is specified all by itself, only step 1 is done, not step 2. After step 1, we have the address to the first element in the array. | ||
The type of the pointer not only allows the compiler to correctly get the contents at an address, but it also allows the compiler to correctly calculate the number of bytes to add when doing pointer arithmetic: | The type of the pointer not only allows the compiler to correctly get the contents at an address, but it also allows the compiler to correctly calculate the number of bytes to add when doing pointer arithmetic: | ||
+ | <code c> | ||
1: int miles[4]; | 1: int miles[4]; | ||
2: miles[0]=342; | 2: miles[0]=342; | ||
Line 473: | Line 661: | ||
5: miles[3]=7; | 5: miles[3]=7; | ||
6: ... *(miles+2) ... | 6: ... *(miles+2) ... | ||
+ | </ | ||
+ | |||
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. | ||
+ | |||
+ | <code c> | ||
1: float gpa[4]; | 1: float gpa[4]; | ||
2: gpa[0]=2.3; | 2: gpa[0]=2.3; | ||
Line 483: | Line 676: | ||
5: gpa[3]=0.0; | 5: gpa[3]=0.0; | ||
6: ... *(gpa+2) ... | 6: ... *(gpa+2) ... | ||
+ | </ | ||
+ | | ||
Image: float array | Image: float array | ||
- | In line (4), gpa is the address of a floating point number, or a float*. By the rules of pointer arithmetic, line (4) will get the contents at address 932. | + | {{: |
+ | In line (4), gpa is the address of a floating point number, or a float*. | ||
+ | |||
+ | <code c> | ||
1: char letter[4]; | 1: char letter[4]; | ||
2: letter[0]=' | 2: letter[0]=' | ||
Line 493: | Line 691: | ||
5: letter[3]=' | 5: letter[3]=' | ||
6: ... *(letter+2) ... | 6: ... *(letter+2) ... | ||
+ | </ | ||
+ | | ||
Image: char array | Image: char array | ||
- | In line (4), letter is the address of an character, or a char*. By the rules of pointer arithmetic, line (4) will get the contents at address 926. | + | {{: |
- | Array Basics Revisited | + | In line (4), letter is the address of an character, or a char*. |
- | Now that we have some insight about C and C++ array quirks, let's take another look at how we started accessing array elements. Prior to diving into this whole step 1 step 2 business with arrays we were initializing, | + | |
+ | ===== Array Basics Revisited ===== | ||
+ | |||
+ | Now that we have some insight about C and C++ array quirks, let's take another look at how we started accessing array elements. | ||
C Code Listing 11 | C Code Listing 11 | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 515: | Line 720: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
C++ Code Listing 11 | C++ Code Listing 11 | ||
+ | |||
+ | <code cpp> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 529: | Line 738: | ||
| | ||
13: } | 13: } | ||
- | On lines (6-9) we are using square braces to access elements for initialization. On line (11) we are using square braces to print out the third element of the array. From the previous section you might have been led to believe that you would print out the third element by putting something like *(age+2). As it turns out, the *(age+2) syntax is the " | + | </ |
+ | |||
+ | On lines (6-9) we are using square braces to access elements for initialization. | ||
Image: blind substitution from array syntax to pointer arithmetic syntax | Image: blind substitution from array syntax to pointer arithmetic syntax | ||
- | This is actually what happens. If you type | + | {{: |
- | monkey[ crackers ], the compiler tries to compile | + | |
+ | This is actually what happens. | ||
+ | |||
+ | <code c> | ||
+ | monkey[ crackers ] | ||
+ | </ | ||
+ | |||
+ | , the compiler tries to compile | ||
+ | |||
+ | <code c> | ||
*(monkey + crackers) | *(monkey + crackers) | ||
+ | </ | ||
I'm not kidding! Let's prove it. | I'm not kidding! Let's prove it. | ||
- | When as a youngster you were asked to add 5+3, no problem. But what about when the numbers were switched and you were asked to add 3+5? Still no problem because of the commutative property of addition. The commutative property of addition holds in C and C++ just like it did when you were a youngster. So, *(age+2) produces exactly the same result as *(2+age). That is, the number of bytes equivalent to two short integers is added to age and then the contents are retrieved at that address. OK, so what? In the previous paragraph I said that when you write something like age[2] the compiler blindly substitutes it into its pointer syntax and compiles *(age+2). So, let's turn it around and prove that all of this array stuff is nothing but simple pointer access with a little addition: | + | When as a youngster you were asked to add 5+3, no problem. |
C Code Listing 12 | C Code Listing 12 | ||
+ | |||
+ | <code c> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 555: | Line 778: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
C++ Code Listing 12 | C++ Code Listing 12 | ||
+ | |||
+ | <code cpp> | ||
1: #include < | 1: #include < | ||
2: | 2: | ||
Line 569: | Line 796: | ||
| | ||
13: } | 13: } | ||
+ | </ | ||
+ | |||
Image: blind substitution from array syntax to pointer arithmetic syntax is equivalent to Image: reversed--blind substitution from array syntax to pointer arithmetic syntax | Image: blind substitution from array syntax to pointer arithmetic syntax is equivalent to Image: reversed--blind substitution from array syntax to pointer arithmetic syntax | ||
- | The commutative property of addition is being taken advantage of in lines (6-9) and line (11). Don't just stare at the code above, type it in, compile it, and run it! Again, this is nothing but simple pointer arithmetic. For example, on line (11), the third element of the age array is accessed. Before compiling 2[age], the compiler first blindly converts it into *(2+age). Pretty cool, eh? | ||
- | We now have more than enough information to answer the question: "Why do arrays in C and C++ start at zero instead of one?". We know that the name of the array produces the address of the beginning of the array; and we know that the beginning of the array is also the address of the first element of the array. So, all of these are exactly the same: | + | |{{: |
+ | |||
+ | |||
+ | The commutative property of addition is being taken advantage of in lines (6-9) and line (11). Don't just stare at the code above, type it in, compile it, and run it! Again, this is nothing but simple pointer arithmetic. | ||
+ | |||
+ | We now have more than enough information to answer the question: "Why do arrays in C and C++ start at zero instead of one?" | ||
+ | |||
+ | * *age | ||
+ | * *(age+0) | ||
+ | * age[0] | ||
- | *age | ||
- | *(age+0) | ||
- | age[0] | ||
The bottom line: Arrays are nothing more than a bunch of variables of the same type using the same name--that name is the address of the beginning of the bunch of variables. | The bottom line: Arrays are nothing more than a bunch of variables of the same type using the same name--that name is the address of the beginning of the bunch of variables. | ||
Re-summarizing, | Re-summarizing, | ||
- | The name of the array. This gets the address of the first element of the array. | + | * The name of the array. This gets the address of the first element of the array. |
- | A number. Pointer arithmetic is used to add this number to the address resulting in the address of one of the elements in the array. Of course if zero is added the address is unchanged (it remains the address of the first element). | + | |
- | Use the * operator to get the contents at that address. Alternately use the square bracket syntax and the compiler will do the addition and content retrieval for you. | + | |
c/basic_arrays.1470406870.txt.gz · Last modified: 2020/07/15 09:30 (external edit)