knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

C Programming: A Modern Approach (Second Edition)

Chapter 24

Answers to Selected Exercises

4. [was #2]

(a)

double try_math_fcn(double (*f)(double), double x, const char *msg)
{
  double result;

  errno = 0;
  result = (*f)(x);
  if (errno != 0) {
    perror(msg);
    exit(EXIT_FAILURE);
  }
  return result;
}

(b)

#define TRY_MATH_FCN(f,x) try_math_fcn(f, x, "Error in call of " #f)

5. [was #4]

int main(void)
{
  char code;

  for (;;) {
    setjmp(env);
    printf("Enter operation code: ");
    scanf(" %c", &code);
    while (getchar() != '\n')   /* skips to end of line */
      ;
    switch (code) {
      case 'i': insert();
                break;
      case 's': search();
                break;
      case 'u': update();
                break;
      case 'p': print();
                break;
      case 'q': return 0;
      default:  printf("Illegal code\n");
    }
    printf("\n");
  }
}

The jmp_buf variable env will need to be global, rather than local to main, so that the function performing the longjmp will be able to supply it as an argument.


Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved.

Google
 
Web knking.com