【发布时间】:2015-05-12 07:15:27
【问题描述】:
在 Dennis Ritchie 的《C 编程语言》一书中, 在 getop 函数中, 他说 s[1]='\0' 为什么他在索引 1 处结束数组?有什么意义和需要?
在后面的部分中,他确实使用了数组的其他部分..
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;
}
【问题讨论】:
-
请正确缩进您的代码。
-
感谢 RedX 现在缩进了
-
while ((s[0] = c = getch()) == ' ' || c == '\t');这样的行让我想哭。 -
@ammoQ 确实如此。然后人们仍然推荐K&R,我完全不知道为什么。看这可怕的代码,不是OP写的,是K&R写的!如果今天有任何菜鸟程序员写出这样的代码,他会立即被解雇。
-
@ammoQ 也不教任何人正确的编程,也不校对或审查他们自己的工作......