Convert two char into one int

M

Mariano

I have this lines:

char *chr;
int integ;
num[0] = '1';
num[1] = '9';

How I could obtain value 19 in the integ variable????
Hi to all.
 
K

Kenny McCormack

I have this lines:

char *chr;
int integ;
num[0] = '1';
num[1] = '9';

How I could obtain value 19 in the integ variable????
Hi to all.

Continue with:

num[2] = 0;
sscanf(num,"%d",&integ):
 
F

Fred Yu

Mariano said:
I have this lines:

char *chr;
int integ;
num[0] = '1';
num[1] = '9';

How I could obtain value 19 in the integ variable????
Hi to all.

integ = atoi(num[0])*10+atoi(num[1]);
 
N

Nick Keighley

Fred said:
"Mariano" <[email protected]>
:8a82a41a-1992-4fa3-9c9b-bc4f6c2b3...@e39g2000hsf.googlegroups.com...
I have this lines:
char *chr;
int integ;
num[0] = '1';
num[1] = '9';
How I could obtain value 19 in the integ variable????
Hi to all.
integ = atoi(num[0])*10+atoi(num[1]);

That's wrong.
num[0] is not a pointer to a string.

and num may not be a valid string, we don't
know if there's a terminating nul character
atoi takes a pointer to a string as an argument.

and has poor error handling
 

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

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top