简体   繁体   中英

The wording of exercise 5.1 in K&R c

Just a note, there is another question on Stack similar to mine, but that question actually asks two questions. One of the questions is unrelated to my question, and it's the only question answered.

I also ask a different question. It can be found below: What am I suppose to do with the zero stored in the array through the pointer?


The exercise gives me a function, int getint(int *) , which takes the address of an array element, converts ASCII digits from the input stream, and then stores them as a decimal number in the int pointer. It returns EOF for end of file, zero if the next input is not a number, and a positive value if the input contains a valid number. The function contains a function ungetch() which pushes a character back onto the input stream.

I don't understand the wording of the following exercise:

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

Is this saying I should push the + or - back on to the input stream or push the character that wasn't a number back onto the input stream? Also, how should I treat the zero stored in the array through the pointer?

Here is the code:

int getint(int *pn)
{
    int c, sign;

    while (isspace(c = getch()))
        ;
    if (!isdigit(c) && c != EOF && c != '+' && c != '-'){
        ungetch(c);
        return 0;
    }
    sign = (c == '-') ? -1 : 1;
    if (c == '+' || c == '-')
        c = getch();
    for (*pn = 0; isdigit(c); c = getch())
        *pn = 10 * *pn + (c - '0');
    *pn *= sign;
    if (c != EOF)
        ungetch(c);
    return c;
}

The exercise is asking you to recognize that there's a subtle bug in the program when you encounter a '+' or '-' but what follows is not a number.

Eg,

+<EOF>
+apple
-banana

As opposed to actual numbers, like

+1
-17

In the current program, if you see the former case in the data stream, you are consuming the '+' and '-', and then telling the user that you saw a number in the stream and that number was 0. But that's not true at all!

Is this saying I should push the + or - back on to the input stream or push the character that wasn't a number back onto the input stream?

Think about what you would expect as a consumer of this function. Like let's say a co-worker gave you this function and you had to use it. What would you want to happen? There are really 2 choices:

  1. Consume the '+' or '-' from the data stream and discard it. Tell the caller with the return value you did not see a number.

  2. Put the '+' or '-' back into the data stream. Tell the caller that you did not see a number.

Chances are, you'd want the function not to throw away data that it did not use. What if you passed a string to the function with this text:

++var;

Is it OK to consume the '+' and just leave +var; ? I don't think so.

So to answer your question directly, push the '+' or '-' back into the stream. Make sure you have put all characters back in the stream that you took out. Return to the user the code that indicates what follows in the stream is not a number.

Also, how should I treat the zero stored in the array through the pointer?

You don't! If you did not see a number, why are you telling the caller that you saw the number 0? Change the code so that you don't set '0' in the array if you did not see a number.

(This one is a little more ambiguous. You could document the function such that the caller must treat pn as garbage if you returned EOF or 0. Then you could write whatever you wanted to pn as long as you simply returned 0. But it's a good exercise to figure out how to modify the function such that you don't touch pn at all if you don't need to.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM