a question abou "atoi"

6

66650755

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'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!!
 
K

Keith Thompson

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.


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.
 
C

CBFalconer

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.


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

all done.
 
U

user923005

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'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;
}
 
M

Mark L Pappin

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.


'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
 
6

66650755

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.


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) (e-mail address removed) <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.
 
K

Kelsey Bjarnason

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

atoi question 7
Problem with atoi() 25
question about atoi 3
atoi query 13
Help with atoi function for a numero program. 4
Return Value of atoi and strtoul 4
Confusing itoa and atoi 2
First time question 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top