在下面的函数中:
[1] = '\0';
?i = 0
,为什么要从s[1]
不从中获取价值s[0]
呢?#define NUMBER '0'
#define MAXSIZE 100
char s[MAXSIZE];
/* 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;
}
该函数似乎将其结果存储在指向字符串的指针中,该字符串以C样式零终止。第一getch
行将结果存储在中s[0]
,如果不是数字或句点,则立即返回。将零存储为第二个字符可确保返回的字符串正确结束-它仅包含一个字符。
第一步之后,您已经有一个有效字符,并将其存储在中s[0]
。因此,所有下一个getch
调用都需要从1开始存储,否则它将覆盖输入的第一个字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句