Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification in getop()

In Dennis Ritchie's "C programming Language" book, In getop func, he states that s[1]='\0' why does he end the array on index 1? What's the significance and need?

In later part he does uses other parts of the array..

int getch(void);
void ungetch(int);

/* getop: get next character or numeric operand */
int getop(char s[])
{
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';

    if (!isdigit(c) && c != '.')
        return c; /* not a number */

    i = 0;
    if (isdigit(c)) /* collect integer part */
        while (isdigit(s[++i] = c = getch()))
            ;

    if (c == '.') /* collect fraction part */
        while (isdigit(s[++i] = c = getch()))
            ;
    s[i] = '\0';

    if (c != EOF)
        ungetch(c);

    return NUMBER;
}
like image 515
ThunderPunch Avatar asked Feb 10 '23 04:02

ThunderPunch


1 Answers

Because the function might return before the remaining input is read, and then s needs to be a complete (and terminated) string.

like image 174
Some programmer dude Avatar answered Feb 13 '23 03:02

Some programmer dude