473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

a question abou "atoi"

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!!
Oct 31 '08 #1
10 4143
66******@qq.com writes:
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 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 doesn't have "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.
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"
Oct 31 '08 #2
66******@qq.com wrote:
>
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';

all done.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 31 '08 #3
On Oct 30, 5:17*pm, 66650...@qq.com wrote:
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}; /* 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;
}

Oct 31 '08 #4
66******@qq.com writes:
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].
'string[2]' is not a string; it is a single character.
The array named 'string' contains a string, initialized as if by

char string[20] = {'1', '2', '3', '4', '5', '\0',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Note: you should not use 'string', or any other identifier beginning
with 'str', as the name of any object or function you define, because
almost all such names are reserved for the use of the implementor,
which you are not.

A string consists of a sequence of zero or more characters followed by
a null character. You need to pass a pointer to such a sequence to
'atoi()'.

Note also: you should probably not use 'atoi()' in any case as it has
a design limitation which has been discussed here recently; use
'strtol()' instead, as it can report its results in more detail.
I'm eager to find an solution.thinks,thinks,thinks!!
To extract the decimal value of a single digit from within a string
such as that shown above:

int value = string[2] - '0';

'value' now contains the value 3. This works because the Standard
guarantees that the representations of the characters '0' through '9'
are sequential. It makes no such guarantee about non-digits, so you
can not portably assume that

('a' + 2) == 'c'

for example.

mlp
Oct 31 '08 #5
On 10ÔÂ31ÈÕ, ÉÏÎç9ʱ32·Ö, Keith Thompson <ks...@mib..orgwrote:
66650...@qq.com writes:
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 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 doesn't have "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.

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"


Thank you very much for your replay,and I've learnt lot from it.
Oct 31 '08 #6
On 31 Oct, 02:05, user923005 <dcor...@connx.comwrote:
* * for (index = 0; index < strlen(string); index++) {
* * * * substring[0] = string[index];
* * * * a = atoi(substring);
* * * * printf("%d\n", a);
* * }
Don't put strlen as a loop condition. Try:

len = strlen( string );
for( i = 0 ; i < len; i++ )
....

Oct 31 '08 #7
William Pursell said:
On 31 Oct, 02:05, user923005 <dcor...@connx.comwrote:
>for (index = 0; index < strlen(string); index++) {
substring[0] = string[index];
a = atoi(substring);
printf("%d\n", a);
}

Don't put strlen as a loop condition. Try:

len = strlen( string );
for( i = 0 ; i < len; i++ )
...
Probably best to explain why. Even though a skilled implementor of the
strlen function may well be able to use some clever tricks to speed it up
on a particular platform, its execution time is nevertheless going to be
approximately proportional to the length of the string, even for fairly
short strings, and *at the point of the call* a compiler will typically be
unable to prove that the string is not modified inside the loop, so it
won't be justified in caching the value. Thus, it will be called on every
single iteration of the loop, leading to O(n*n) behaviour - quadratic
performance, i.e. fairly dire. But *you* know the string is not modified
inside the loop, so you *are* justified in caching the value - and should
do so, as it reduces quadratic to linear.

--
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
Oct 31 '08 #8
66******@qq.com wrote:
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?
You don't need to - string[2] is already an integer type.

int x = string[2];

does the trick.
int a;
char string[7]="111111";

a=atoi(string[3]);
atoi takes a string, string[2] is a character - which is an integer type.
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.
You're using a C++ compiler, don't do that if you're compiling C, the
two languages are different.
Oct 31 '08 #9
On Fri, 31 Oct 2008 11:23:23 +0000, Mark McIntyre wrote:
66******@qq.com wrote:
>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?

You don't need to - string[2] is already an integer type.

int x = string[2];

does the trick.
True, but the value I get here when doing this is 51; I suspect he wanted
the value 3.

int x = string[2] - '0'; would do the trick.

Oct 31 '08 #10
Kelsey Bjarnason wrote:
On Fri, 31 Oct 2008 11:23:23 +0000, Mark McIntyre wrote:
>66******@qq.com wrote:
>>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?
You don't need to - string[2] is already an integer type.

int x = string[2];

does the trick.

True, but the value I get here when doing this is 51; I suspect he wanted
the value 3.

int x = string[2] - '0'; would do the trick.
I was kinda leaving that as an exercise for the reader, given that its
an FAQ... :-)

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Oct 31 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
4
by: salem.ganzhorn | last post by:
#include <map> #include <iostream> using namespace std; int main( int argc, const char * argv ) {
1
by: entropy123 | last post by:
Hey all, In an effort to solve a sticky - for me - problem I've picked up Sedgewick's 'Algorithm's in C'. I've tried working through the first few problems but am a little stumped when he refers...
7
by: Przemo Drochomirecki | last post by:
hi, SCANF is not well described in my books, i'd like to do this stuff: 1) read all numbers from a line, separated by #32 2) read all numbers from a line, separaed by ',' 3) read whole line ...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
13
by: Dave win | last post by:
howdy.... plz take a look at the following codes, and tell me the reason. 1 #define swap(a,b) a=a^b;b=b^a;a=a^b 2 3 int main(void){ 4 register int a=4; 5 register int b=5;...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
6
by: Gaijinco | last post by:
I was trying to solve: acm.uva.es/p/v101/10191.html I did this code: #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cctype>
3
by: decidence | last post by:
Ok, this is an odd problem for me, I know how to stop it from happening but I get incorrect functionality when I do. This is a run time error that occurs when the program is shut down, I get the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.