的atoi [英] atoi

查看:134
本文介绍了的atoi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

你能帮助我吗?为什么这个警告出现在下一个简单的代码中?

警告:传递?atoi的参数1?在没有强制转换的情况下从整数

生成指针。

#include< stdio.h>

#include< stdlib。 h>

#include< string.h>


int i;

char line [100];


int main()

{

printf(" Enter line:\ n");

fgets(line,sizeof(line),stdin);

line [strlen(line)-1] =''\ 0'';

for(i = 0; i< strlen(line); i ++)

{

printf(" line [%d] =%d \ n",i,atoi( line [i]));

}

退出(0);

}


在排除时:

$ ./test

输入行:

这是一个测试

分段错误

解决方案

./ test

输入行:

这是一个测试

分段错误


Nezhate说:


大家好,

你能帮助我吗?为什么这个警告出现在下一个简单的代码中?

警告:传递?atoi的参数1?在没有强制转换的情况下从整数

生成指针。



那是因为你传递atoi一个字符(这是一种整数)

而不是字符串(它作为指针传递。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int i;

char line [100];



将这些设为本地。


int main()

{

printf(" Enter line:\ n");

fgets(line,sizeof(line),stdin);

line [ strlen(line)-1] =''\ 0'';

for(i = 0; i< strlen(line); i ++)

{

printf(" line [%d] =%d \ n",i,atoi(line [i]));

}


我想*你正试图这样做:


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main()

{

size_t i = 0;

char line [100] = {0};

size_t len = 0;


printf("输入行(不超过98个字符):\ n");

if(fgets(line,sizeof(line),stdin)!= NULL)

{

len = strlen(line);

line [len - 1] =''\ 0'';

for(i = 0; i& lt; len; i ++)

{

printf(" line [%d] =%d \ n",(int)i,atoi(line + i ));

}

}

返回0;

}


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


Nezhate写道:


大家好,

你能帮助我吗?为什么这个警告出现在下一个简单的代码中?

警告:传递?atoi的参数1?在没有强制转换的情况下从整数

生成指针。

#include< stdio.h>

#include< stdlib。 h>

#include< string.h>


int i;

char line [100];


int main()

{

printf(" Enter line:\ n");

fgets(line,sizeof(line),stdin);

line [strlen(line)-1] =''\''';



这不是必需的。 Fgets将始终使用null

字符终止输入。


for(i = 0; i< strlen(line); i ++)

{

printf(" line [%d] =%d \ n",i,atoi(line [i]));



函数atoi需要一个char *参数。在这里你传递它

char对象,它们实际上只是C中的整数。这就是你的

编译器所抱怨的。


}

退出(0);

}


在排除时:

Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]=''\0'';
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
}
exit(0);
}

when excuting:
$ ./test
Enter line:
this is a test
Segmentation fault

解决方案

./test
Enter line:
this is a test
Segmentation fault


Nezhate said:

Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.

That''s because you''re passing atoi a char (which is a kind of integer)
rather than a string (which is passed as a pointer).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
char line[100];

Make these local.

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]=''\0'';
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
}

I *think* you''re trying to do this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
size_t i = 0;
char line[100] = {0};
size_t len = 0;

printf("Enter line (no more than 98 characters):\n");
if(fgets(line,sizeof(line),stdin) != NULL)
{
len = strlen(line);
line[len - 1]=''\0'';
for (i = 0; i < len; i++)
{
printf("line[%d]=%d\n", (int)i, atoi(line + i));
}
}
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Nezhate wrote:

Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]=''\0'';

This is not needed. Fgets will always terminate the input with a null
character.

for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));

The function atoi expects a char * parameter. Here you are passing it
char objects which are really just integers in C. That''s what your
compiler is complaining about.

}
exit(0);
}

when excuting:


这篇关于的atoi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆