section 1.6: Arrays
When they say ``as reflected in the for loops that initialize and print the array,'' they're referring to the fact that the vast majority of for loops in C look like this:
for(i = 0; i < 10; ++i)and count from 0 to 9. The loop
for(i = 1; i <= 10; ++i)would count from 1 to 10, but loops like this are comparatively rare. (In fact, whenever you see either ``= 1'' or ``<='' in a for loop, it's an indication that something unusual is going on which you'll want to be aware of, and it may even be a bug.)
They've started going a little fast here, so read up if they're losing you. What's this magic expression c-'0' that they're using as an array subscript? Remember, as we saw first on page 19, that characters in C are represented by small integers corresponding to their values in the machine's character set. In ASCII, which most machines use, 'A' is character code 65, '0' (zero) is code 48, '9' is code 57, and all the other characters have their own values which I won't bother to list. If we've just read the character '9' from the file, it has value 57, so c-'0' is 57 - 48 which is 9, and we'll increment cell number 9 in the array, just like we want to. Furthermore, even if we're not using a machine which uses ASCII, by subtracting '0', we'll always subtract whatever the right value is to map the characters from '0' to '9' down to the array cell range 0 to 9.
No comments:
Post a Comment