Archived

This topic is now archived and is closed to further replies.

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

This topic is 6111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites