Chapter 5: Pointers and Arrays is the chapter C beginners fear most — and with reason. K&R covers pointer arithmetic, the equivalence between pointers and arrays, pointers to functions, and multi-dimensional arrays. The exercises build real utilities: getfloat, strncpy/strncat/strncmp from scratch, a tail command, a sort program with -r/-n/-f/-d flags, and a full C declaration parser (dcl/undcl).
These are worked solutions to all 20 exercises. Each solution compiles cleanly with gcc -ansi -Wall.
Book: The C Programming Language, 2nd Ed — Kernighan & Ritchie | All chapters index
Exercises
-
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. -
Exercise 5-2. Write
getfloat, the floating-point analog ofgetint. What type doesgetfloatreturn as its function value? -
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. -
Exercise 5-4. Write the function
strend(s,t), which returns 1 if the stringtoccurs at the end of the strings, and zero otherwise. -
Exercise 5-5. Write versions of the library functions
strncpy,strncat, andstrncmp, which operate on at most the first n characters of their argument strings. -
Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include
getline,atoi,itoa,reverse,strindex, andgetop. -
Exercise 5-7. Rewrite
readlinesto store lines in an array supplied bymain, rather than callingallocto maintain storage. How much faster is the program? -
Exercise 5-8. There is no error-checking in
day_of_yearormonth_day. Remedy this defect. -
Exercise 5-9. Rewrite the routines
day_of_yearandmonth_daywith pointers instead of indexing. -
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×(3+4). -
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. -
Exercise 5-12. Extend
entabanddetabto accept the shorthandentab -m +nto mean tab stops every n columns, starting at column m. Choose convenient (for the user) default behavior. -
Exercise 5-13. Write the program
tail, which prints the last n lines of its input. By default, n is 10, but it can be changed by an optional argument, so thattail -nprints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. -
Exercise 5-14. Modify the sort program to handle a
-rflag, which indicates sorting in reverse (decreasing) order. Be sure that-rworks with-n. -
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. -
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. -
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.
-
Exercise 5-18. Make
dclrecover from input errors. -
Exercise 5-19. Modify
undclso that it does not add redundant parentheses to declarations. -
Exercise 5-20. Expand
dclto handle declarations with function argument types, qualifiers likeconst, and so on.
See also: K&R C — All Chapters Index | Complete C Programs List
1 comment on “K&R C Chapter 5 Exercise Solutions — Pointers and Arrays”
Thanks for providing the information at one place and I am here to discuss about a simple topic in mathematics that is rational expression,Rational expressions is known as an expression that is the ratio of two polynomials.It is called as rational because one number is divided by the other that is like a ratio.
lcm calculator