5 - Pointers and Arrays
Exercise 5-1
As written,
getinttreats a+or-not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input. [1]
My solution
tbd
Official solution
tbd
Exercise 5-2
Write
getfloat, the floating-point analog ofgetint. What type doesgetfloatreturn as its function value? [1]
My solution
tbd
Official solution
tbd
Exercise 5-3
Write a pointer version of the function
strcatthat we showed in Chapter 2:strcat(s, t)copies the stringtto the end ofs. [1]
My solution
tbd
Official solution
tbd
Exercise 5-4
Write the function
strend(s, t), which returns1if the stringtoccurs at the end of the strings, and zero otherwise. [1]
My solution
tbd
Official solution
tbd
Exercise 5-5
Write versions of the library functions
strncpy,strncat, andstrncmp; which operate on at most the firstncharacters of their argument strings. For example,strncpy(s, t, n)copies at mostncharacters ofttos. Full descriptions are in Appendix B. [1]
My solution
tbd
Official solution
tbd
Exercise 5-6
Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include
getline(Chapters 1 and 4),atoi,itoa, and their variants (Chapters 2, 3, and 4),reverse(Chapter 3), andstrindexandgetop(Chapter 4). [1]
My solution
tbd
Official solution
tbd
Exercise 5-7
Rewrite
readlinesto store lines in an array supplied bymain, rather than callingallocto maintain storage. How much faster is the program? [1]
My solution
tbd
Official solution
tbd
Exercise 5-8
There is no error checking in
day_of_yearormonth_day. Remedy this defect. [1]
My solution
tbd
Official solution
tbd
Exercise 5-9
Rewrite the routines
day_of_yearandmonth_daywith pointers instead of indexing. [1]
My solution
tbd
Official solution
tbd
Exercise 5-10
Write the program
expr, which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example,expr 2 3 4 + *evaluates
2 x (3 + 4). [1]
My solution
tbd
Official solution
tbd
Exercise 5-11
Modify the programs
entabanddetab(written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments. [1]
My solution
tbd
Official solution
tbd
Exercise 5-12
Extend
entabanddetabto accept the shorthandentab -m +nto mean tab stops every
ncolumns, starting at columnm. Choose convenient (for the user) default behavior. [1]
My solution
tbd
Official solution
tbd
Exercise 5-13
Write the program
tail, which prints the lastnlines of its input. By default,nis10, let us say, but it can be changed by an optional argument, so thattail -nprints the last
nlines. The program should behave rationally no matter how unreasonable the input or the value ofn. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size. [1]
My solution
tbd
Official solution
tbd
Exercise 5-14
Modify the sort program to handle a
-rflag, which indicates sorting in reverse (decreasing) order. Be sure that-rworks with-n. [1]
My solution
tbd
Official solution
tbd
Exercise 5-15
Add the option
-fto fold upper and lower case together, so that case distinctions are not made during sorting; for example,aandAcompare equal. [1]
My solution
tbd
Official solution
tbd
Exercise 5-16
Add the
-d("directory order") option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with-f. [1]
My solution
tbd
Official solution
tbd
Exercise 5-17
Add a field-handling capability, so sorting may be done on fields within lines, each field sorted according to an independent set of options. (The index for this book was sorted with
-dffor the index category and-nfor the page numbers.) [1]
My solution
tbd
Official solution
tbd
Exercise 5-18
Make
delrecover from input errors. [1]
My solution
tbd
Official solution
tbd
Exercise 5-19
Modify
undclso that it does not add redundant parentheses to declarations. [1]
My solution
tbd
Official solution
tbd
Exercise 5-20
Expand
delto handle declarations with function argument types, qualifiers likeconst, and so on. [1]
My solution
tbd
Official solution
tbd