使用atoi()函数将字符串转换为整数 [英] convertin string to integer using atoi() function
问题描述
请先考虑以下代码......
int getint(char *);
int main()
{
char name [80];
printf("输入字符串\ n");
scanf("%s" ;,姓名);
getint(姓名);
}
int getint(char * ptr)
{
int x = 0;
x = atoi(* ptr);
printf("%d \ n",x) ;
}
当我在LINUX O / SI AM上运行这个分段时故障
你能帮我解决问题
谢谢你...
Sir please consider the following code......
int getint(char *);
int main ()
{
char name[80];
printf("enter the string\n");
scanf("%s",name);
getint(name);
}
int getint(char *ptr)
{
int x=0;
x=atoi(*ptr);
printf("%d\n",x);
}
WHEN I RUN THIS ON LINUX O/S I AM GETTING "SEGMENTATION" FAULT
COULD YOU PLEASE HELP ME WITH THE PROBLEM
THANK YOU...
推荐答案
你正在路过aoi的参数类型错误,如果你在哪里包含正确的标题,编译器就会为你找到这个错误。
You are passing the wrong parameter type to atoi, if you where to include the correct headers the compiler would pick up this mistake for you.
Char * ptr代表'你的字符串所以它'是'熟悉ptr []。当你想要打印整个数组你不要指定数组元素而不是像ptr一样。这个指针也一样。所以:
x = atoi(ptr);
应该有效。
Savage
Char *ptr represent''s ur string so it''s simmiliar to ptr[].When u want to print whole array u don''t specify array element instead u put it like ptr.The same is with this pointer.So:
x=atoi(ptr);
should work.
Savage
试试atoi (ptr)而不是atoi(* ptr)
try atoi(ptr) instead of atoi(*ptr)
这篇关于使用atoi()函数将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!