我有一個從K & R C 2nd版的atoi()函數的例子的問題。只能使用從0到9的字符。但是在我的程序的邏輯中,我做錯了什麼。在K&R C書中的atoi函數
所以有這樣的功能:
#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) ===|
該錯誤消息是chrystal清除。 – 2013-04-10 18:25:18
@JimBalter那個錯誤信息是chrystal清楚的。事實上,固定的,我是以錯誤的方式解析價值。編輯主帖。 – 2013-04-10 18:49:36
抱歉拼寫錯誤,應該是「水晶」。請不要編輯您的代碼,因爲這意味着答案沒有意義。更正應作爲附錄添加。 – 2013-04-10 22:01:38