Question: Exercise 5-2 7410:39 29112929 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get next integer from input

Exercise 5-2 Exercise 5-2 7410:39 29112929 @ 100% + SECTION 5.3 POINTERS AND ARRAYS

97 #include int getch(void); void ungetch(int); 1. getint: get next integer from

7410:39 29112929 @ 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 (isspacec - getch())) 1+ skip white space +/ if (lisdigit(c) && c I= EOF && C. && != '') ungetch(c): / it's not a number / return 0; } sign = (c ='')? -1 : 1; 1f (c == ' 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 swapla, b): where the swap function is defined as void aplint *, int y) WRONG int temp: 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: sapka, kb); 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 3 yold swaplint px, intoy) / Interchange px and opy / int tempi 100 28 tesp px: px - pyt .py - tempi 1 Pictorially: in caller: a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!