Question: getfloat in C excise 2 only 7410:39 211429 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get

getfloat in C
excise 2 only
7410:39 211429 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get next integer from input into .pn / int getint(int .pn) int c, sign; while (isspace(c = getch()) 1+ skip white space +/ if (lisdigit(c) && c I- EOF & CIE'+'&&C I...) ungetch(c): / it's not a number / return 0; } sign = (c ='') ?-1 : 1; if (c == 't II c == '-') c = getch(); for (.pn .0; isdigit(c); c. getch()) .pn . 10 . .pn . (c-'0'); *pn sign: if (c != BOP) ungetch(c); return c; Throughout getint, *pn is used as an ordinary int variable. We have also used getch and ungetch (described in Section 4.3) so the one extra character that must be read can be pushed back onto the input. 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. Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value? O 288 5.3 Pointers and Arrays In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any opera- tion that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, EF 12:31 212 @ 100% + : 5.2 Pointers and Function Arguments Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order elements with a function called swap. It is not enough to write svapla, b): where the swap function is defined as void swap(int n, int y) MRONG / int tempi temp - x: X-Y: y temp: > Because of call by value, swap can't affect the arguments a and b in the rou tine that called it. The function above only swaps copies of a and b. The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: svapia, b); Since the operator & produces the address of a variable, &a is a pointer to a in swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them. 96 POINTERS AND ARRAYS CHAPTER yold swap(int *px, intoy) / interchange px and opy / int tempi 100 temppx: *px - pyt py tempi 28 Pictorially: in caller: a 7410:39 211429 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get next integer from input into .pn / int getint(int .pn) int c, sign; while (isspace(c = getch()) 1+ skip white space +/ if (lisdigit(c) && c I- EOF & CIE'+'&&C I...) ungetch(c): / it's not a number / return 0; } sign = (c ='') ?-1 : 1; if (c == 't II c == '-') c = getch(); for (.pn .0; isdigit(c); c. getch()) .pn . 10 . .pn . (c-'0'); *pn sign: if (c != BOP) ungetch(c); return c; Throughout getint, *pn is used as an ordinary int variable. We have also used getch and ungetch (described in Section 4.3) so the one extra character that must be read can be pushed back onto the input. 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. Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value? O 288 5.3 Pointers and Arrays In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any opera- tion that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, EF 12:31 212 @ 100% + : 5.2 Pointers and Function Arguments Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order elements with a function called swap. It is not enough to write svapla, b): where the swap function is defined as void swap(int n, int y) MRONG / int tempi temp - x: X-Y: y temp: > Because of call by value, swap can't affect the arguments a and b in the rou tine that called it. The function above only swaps copies of a and b. The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: svapia, b); Since the operator & produces the address of a variable, &a is a pointer to a in swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them. 96 POINTERS AND ARRAYS CHAPTER yold swap(int *px, intoy) / interchange px and opy / int tempi 100 temppx: *px - pyt py tempi 28 Pictorially: in caller: a