Solution for Exercise 19-19
ex1919
#include <stdio.h> int main() { char *fruit[] = { "watermelon", "banana", "pear", "apple", "coconut", "grape", "blueberry" }; int x; for(x=0;x<7;x++) { while( putchar(*(*(fruit+x))++)) ; putchar('\n'); } return(0); }
Output
watermelon
banana
pear
apple
coconut
grape
blueberry
Notes
* Here's the translation for Line 18:
is quite similar to:
Where pa
holds the base address of a string. So while(*pa++)
outputs a string just as while(*(*(fruit+x))++)
because like pa
, *(fruit+x)
translates to an address. The parentheses are required.
* Here's how *(*(fruit+x))++
evaluates:
* Don't fret if you didn't arrive at this solution. This exercise wasn't an easy one. Here's another way to work the while loop:
This method uses int variable a
as the offset to the string. This solution looks more cumbersome, but it may help you understand what's going on — or if you devised a similar solution by yourself, so be it.
* This type of pointer construction doesn't often find itself into "real" C programs. It's just easier to use printf() to output the strings.
Copyright © 1997-2024 by QPBC.
All rights reserved