K&R C Chapter 5 Exercise Solutions — Pointers and Arrays

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

  1. Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.

    Solution to Exercise 5-1

  2. Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value?

    Solution to Exercise 5-2

  3. Exercise 5-3. Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s.

    Solution to Exercise 5-3

  4. Exercise 5-4. Write the function strend(s,t), which returns 1 if the string t occurs at the end of the string s, and zero otherwise.

    Solution to Exercise 5-4

  5. Exercise 5-5. Write versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings.

    Solution to Exercise 5-5

  6. 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, and getop.

    Solution to Exercise 5-6

  7. Exercise 5-7. Rewrite readlines to store lines in an array supplied by main, rather than calling alloc to maintain storage. How much faster is the program?

    Solution to Exercise 5-7

  8. Exercise 5-8. There is no error-checking in day_of_year or month_day. Remedy this defect.

    Solution to Exercise 5-8

  9. Exercise 5-9. Rewrite the routines day_of_year and month_day with pointers instead of indexing.

    Solution to Exercise 5-9

  10. 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).

    Solution to Exercise 5-10

  11. Exercise 5-11. Modify the programs entab and detab (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.

    Solution to Exercise 5-11

  12. Exercise 5-12. Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m. Choose convenient (for the user) default behavior.

    Solution to Exercise 5-12

  13. 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 that tail -n prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n.

    Solution to Exercise 5-13

  14. Exercise 5-14. Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n.

    Solution to Exercise 5-14

  15. Exercise 5-15. Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal.

    Solution to Exercise 5-15

  16. 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.

    Solution to Exercise 5-16

  17. 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.

    Solution to Exercise 5-17

  18. Exercise 5-18. Make dcl recover from input errors.

    Solution to Exercise 5-18

  19. Exercise 5-19. Modify undcl so that it does not add redundant parentheses to declarations.

    Solution to Exercise 5-19

  20. Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const, and so on.

    Solution to Exercise 5-20

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>