I'm studying K&R book. Currently i'm reading function getop() at p.78. I do understand the code but i need clarifications about 2 things.
The code of getop() is as follows:
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;
}
My question is about: s[0]
in:
while ((s[0] = c = getch()) == ' ' || c == '\t')
The idea behind the while loop is to skip spaces and horizontal tab, so why are we saving 'c' in s[0]? Why the authors didn't simply write:
while (c= getch() == ' ' || c == '\t')
We are not going to use spaces and tabs later on, why do we need to save c in s[0]
for? What is the need for s[0]
here?
My second question is about:
s[1] = '\0';
Why are we assigning '\0' (end of string) to s[1]
here?
I have read some of the previous answers posted on stackoverflow.com about it but i'm not totally convinced!
The accepted answer about the above question is: "Because the function might return before the remaining input is read, and then s needs to be a complete (and terminated) string."
Ok. But what if input has one white space at the beginning and followed by an operand or operator? In this case, s[1] = '\0'
will close the string too early? isn't it?