我对 K&R C 第 2 版中的 atoi() 函数示例有一些问题。只能使用 0 到 9 的字符。但是在我的程序逻辑的某个地方我做错了。
所以里面有这个功能:
#include <stdio.h>
int atoi(char s[]);
int main()
{
int i;
char ch;
char co[50];
int ci[50];
while(ch != EOF )
{
for(i=0;i<50-1 && (ch=getchar()) != EOF && ch != '\n';++i)
{
co[i] = ch;
/*ci[i] = atoi(co[i]);*/ /*bugged*/
ci[i] = atoi(co);
printf("%d \n",ci[i]);
}
if(ch == '\n')
{
co[i] = '\n';
}
++i;
co[i] = '\0';
}
return(0);
}
/* as in the book: */
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
{
n = 10 * n + (s[i] - '0');
}
return(n);
}
以下是我得到的错误:
|In function 'main':
19|warning: passing argument 1 of 'atoi' makes pointer from integer without a cast [enabled by default]
3|note: expected 'char *' but argument is of type 'char'
||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|