sorry for all the topics...but I am having trouble with 'atoi'

Started by
6 comments, last by Mccy257 23 years ago
Can someone please explain atoi in plain english and why the heck you need to have char and int variables with the atoi sequence. This is C by the way. I don''t really understand the whole char/int/atoi deal. Thanks in advance
Advertisement
Well as you know atoi means AlphaToInteger. So it converts a character to an int. Ok suppose you have in your program a function that loads data from a txt file. And this function loads in as a string every line in the file at a time. And if the string was something like "AGE: 5" and you needed that 5 to be stored as an integer in some variable, you couldnt just parse the string and take the 5 out and store it directly into your integer variable. What would happen is a typecast would take place which would convert the 5 into its ASCII value equivelent which is 53. As you can see, that would throw your program off. So by using the atoi function, it would convert the character ''5'' to the number 5. Which might be what you wanted.

-SirKnight
Thanks for your response. I am just having trouble remembering where to put the variable of int and char into these lines:

gets(char);
int=atoi(char);

This formula just won't stick in my head!!

Edited by - Mccy257 on March 21, 2001 6:27:55 PM
Heres an example that may help:

char *stuff;
int number = 0;

gets( stuff );
number = atoi( stuff );

printf( "%d", number );

Now im assuming by passing a pointer to gets works. I have never used gets. Just scanf and fscanf.

-SirKnight
quote:Original post by SirKnight

Heres an example that may help:

char *stuff;
int number = 0;

gets( stuff );
number = atoi( stuff );

printf( "%d", number );

Now im assuming by passing a pointer to gets works. I have never used gets. Just scanf and fscanf.

-SirKnight


No, gets has to point to an array, not to a uninitialized pointer. Also, you should use fgets with stdin so that you can make sure there isn''t a buffer overflow. Also, in many cases you have to flush stdin (no always though) so that fgets will work right. So your code should be:
char stuff[11];/* The biggest 32-bit value is 4 billion something, so 10 chars for numbers one for a NULL */int number = 0;fflush(stdin); /* This isn''t always needed */fgets(stuff,11,stdin);number = atoi( stuff);printf("%d", number); 



"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/
Ah ok, thanks for the correction on that.

-SirKnight
Some people only use the C libraries, some people only use the C++ ones; we correct each other .

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/
In C programming for dummies, sometimes the guy uses atoi to convert strings into values and sometimes he doesn''t. When do you use ATOI and please include a few examples. Thanks

This topic is closed to new replies.

Advertisement