一个问题abou“atoi” [英] a question abou "atoi"

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

问题描述

首先,感谢所有回答我上一个问题的人。


如果char string [20] =" 12345";


如何使用

atoi将字符串[2](即3)转换为int?我只想转换字符串[2],而不是其他字符串[i]。


我写了这些句子:


int a;

char string [7] =" 111111";


a = atoi(string [3]);


然而,编译器说:

错误C2664:''atoi'':无法将参数1从''char''转换为''const

char *''

从积分类型转换为指针类型需要

reinterpret_cast,C风格演员或函数式演员。


我渴望找到解决方案。思考,思考,思考!!

First,thanks for all who have answered my last question.

if char string[20]="12345";

how could I convert the string[2](that is "3") to an int by using
atoi? I only want to convert string[2],not other string[i].

I''ve written these sentences:

int a;
char string[7]="111111";

a=atoi(string[3]);

however,the compiler said:
error C2664: ''atoi'' : cannot convert parameter 1 from ''char'' to ''const
char *''
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast.

I''m eager to find an solution.thinks,thinks,thinks!!

推荐答案

66 ****** @ qq.com 写道:

首先,感谢所有有回答的人我的上一个问题。


如果char string [20] =" 12345" ;;


怎么能转换字符串[2](使用

对int进行3的atoi?我只想转换字符串[2],而不是其他字符串[i]。
First,thanks for all who have answered my last question.

if char string[20]="12345";

how could I convert the string[2](that is "3") to an int by using
atoi? I only want to convert string[2],not other string[i].



atoi很危险;躲开它。它可以在

错误上调用未定义的行为,即可能发生任意不好的事情。我们最近在这里讨论过什么类型的错误,溢出与不良形式的字符串,可能会导致什么后果,但最重要的是它是什么?除非你先仔细检查参数,否则
不能安全使用。

strtol()可能有点难用,但它更容易使用

安全。

atoi is dangerous; avoid it. It can invoke undefined behavior on
error, i.e., arbitrarily bad things can happen. We had a discussion
here recently about what kinds of errors, overflow vs. ill-formed
strings, can cause what consequences, but the bottom line is that it
can''t be used safely unless you carefully check the argument first.
strtol() can be a bit harder to use, but it''s much easier to use
safely.


我写了这些句子:
I''ve written these sentences:



C没有句子。

C doesn''t have "sentences".


int a;

char string [7] =" 111111";


a = atoi(字符串[3]);


然而,编译器说:

错误C2664:'' atoi'':无法将参数1从''char''转换为''const

char *''

从积分类型转换为指针类型需要

reinterpret_cast,C风格的演员表或功能风格的演员。
int a;
char string[7]="111111";

a=atoi(string[3]);

however,the compiler said:
error C2664: ''atoi'' : cannot convert parameter 1 from ''char'' to ''const
char *''
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast.



错误消息暗示您正在使用C ++编译器。决定

您想要使用哪种语言,并发布到相应的组。

大多数C ++编译器都可以作为C编译器调用;如果你想写一下
C,请了解如何为你做这件事。 (有时命名你的来源
带有.c后缀的
文件就足够了。)


字符串是一系列字符,以及包括a

终止空字符(''\ 0'')。单个字符不是字符串

(除非它是''\ 0'',但这在这里没用)。 atoi()期望一个指向字符串的

指针;传递它一个字符是没有意义的。

忽略转换为指针类型的说法;那不是

你想做什么。


如果你真的想从字符串中提取单个字符并且

将*作为字符串*传递给atoi(或者,最好是strtol),你可以

声明一个2个字符的数组,将所需的字符复制到第一个

元素,并将第二个元素设置为''\0''。

数组的内容现在是一个有效的字符串,您可以将它(或者更确切地说是一个

指针)传递给atoi或strtol。


但是有一种更简单的方法。由于语言保证,对于

你使用的任何字符集,数字''''到''9''具有

连续表示,保证以下内容:


''0'' - ''0''== 0

''1'' - ''0''= = 1

''2'' - ''0''== 2

...

''9'' - ' '0''== 9


0的值很可能是你系统上的48,或者如果你的话可能是240

''正在使用IBM大型机,但上述内容仍然有保证。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

The error message implies that you''re using a C++ compiler. Decide
which language you want to use, and post to the appropriate group.
Most C++ compilers can be invoked as C compilers; if you want to write
C, find out how to do this for yours. (Sometimes naming your source
file with a ".c" suffix is sufficient.)

A string is a sequence of characters, terminated by and including a
terminating null character (''\0''). A single character isn''t a string
(unless it''s a ''\0'', but that''s not useful here). atoi() expects a
pointer to a string; passing it a single character doesn''t make sense.
Ignore what it says about converting to a pointer type; that''s not
what you want to do.

If you really want to extract a single character from a string and
pass it *as a string* to atoi (or, preferably, to strtol), you could
declare a 2-character array, copy the desired character to the first
element, and set the second element to ''\0''. The contents of the
array are now a valid string, and you can pass it (or, rather, a
pointer to it) to atoi or strtol.

But there''s an easier way. Since the language guarantees that, for
whatever character set you''re using, the digits ''0'' through ''9'' have
contiguous representations, the following are guaranteed:

''0'' - ''0'' == 0
''1'' - ''0'' == 1
''2'' - ''0'' == 2
...
''9'' - ''0'' == 9

The value of ''0'' is most likely 48 on your system, or it might be 240
if you''re using an IBM mainframe, but the above are still guaranteed.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


66 ****** @ qq.com 写道:

>

首先,感谢所有回答我上一个问题的人。


如果char string [20] =" 12345";


如何使用

atoi将字符串[2](即3)转换为int?我只想转换字符串[2],而不是其他字符串[i]。
>
First,thanks for all who have answered my last question.

if char string[20]="12345";

how could I convert the string[2](that is "3") to an int by using
atoi? I only want to convert string[2],not other string[i].



int charval;

...

charval = string [2] - ''0'' ;


全部完成。


-

[邮件]:Chuck F(cinefalconer at maineline dot net )

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

int charval;
...
charval = string[2] - ''0'';

all done.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


10月30日,5:17 * pm,66650 ... @ qq.com写道:
On Oct 30, 5:17*pm, 66650...@qq.com wrote:

首先,感谢所有回答我的人最后一个问题。


if * * * * * * * * * * char string [20] =" 12345";


how我可以使用

atoi将字符串[2](即3)转换为int吗?我只想转换字符串[2],而不是其他字符串[i]。


我写了这些句子:


* * * * int a;

* * * * char string [7] =" 111111";


* * * * a = atoi(string [3]);


然而,编译器说:

错误C2664:''atoi'':无法从''char'转换参数1' to''const

char *''

* * * *从积分类型到指针类型的转换需要

reinterpret_cast,C风格的演员表或功能风格的演员表。
First,thanks for all who have answered my last question.

if * * * * * * * * * *char string[20]="12345";

how could I convert the string[2](that is "3") to an int by using
atoi? I only want to convert string[2],not other string[i].

I''ve written these sentences:

* * * * int a;
* * * * char string[7]="111111";

* * * * a=atoi(string[3]);

however,the compiler said:
error C2664: ''atoi'' : cannot convert parameter 1 from ''char'' to ''const
char *''
* * * * Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast.



#include< string.h>

#include< stdio.h>


int main(void)

{

int a;

char string [7] =" 123456" ;;

char substring [2] = {0}; / *现在包含[0] [0] * /

int index;


for(index = 0; index< strlen(string); index ++ ){

substring [0] = string [index];

a = atoi(substring);

printf("%d \ n",a);

}

返回0;

}

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

int main(void)
{
int a;
char string[7] = "123456";
char substring[2] = {0}; /* now contains [0][0] */
int index;

for (index = 0; index < strlen(string); index++) {
substring[0] = string[index];
a = atoi(substring);
printf("%d\n", a);
}
return 0;
}

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

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