Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How standard atoi works?

Reply
Thread Tools

How standard atoi works?

 
 
jigsaw@gmail.com
Guest
Posts: n/a
 
      01-15-2006
hi all,

Hereunder two versions of reading a word from stdin stream:

/* code starts */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char *
getnumber1(void); /* return a word from stdin */

void
getnumber2(char *); /* read a word from stdin and store it in the char*
*/

int main()
{
int i = 0;
char w[20];

i = atoi(getnumber1());
printf("getnumber1 = %d\n", i);
i = 0;
getnumber2(w);
i = atoi(w);
printf("getnumber2 = [%s]\n", w);
printf("getnumber2 = %d\n", i);
return 0;
}

char *
getnumber1(void)
{
char w[20];
char *p;
int c;

p = w;
while (isspace(c = getchar()))
;
if (c != EOF)
*p++ = c;
for (; {
if (isspace(c = getchar())) {
ungetc(c, stdin);
break;
}
*p++ = c;
}
*p = '\0';
printf("getnumber1 = [%s]\n", w);
return w;
}

void
getnumber2(char *w)
{
char *p;
int c;

p = w;
while (isspace(c = getchar()))
;
if (c != EOF)
*p++ = c;
for (; {
if (isspace(c = getchar())) {
ungetc(c, stdin);
break;
}
*p++ = c;
}
*p = '\0';
return;
}
/* code ends */

I use bc++5.5 as compiler.
the input and output of this small program is like this:

D:\c\scan>getnum
12
getnumber1 = [12]
getnumber1 = 1
12
getnumber2 = [12]
getnumber2 = 12

You can see that results of getnumber1 and getnumber2 are the same.
(12)
but the output of atoi are diffrent.
Can anyone do me a favor to explain the diffrence?

Thanx and
Regards,

jigsaw

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-15-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> wrote:
>int main()
>{
> int i = 0;
> char w[20];


> i = atoi(getnumber1());


>char *
>getnumber1(void)
>{
> char w[20];
> char *p;
> int c;


> p = w;


> return w;


When you return a char[n], the result is not that the character string
contents are somehow transfered back: the results are that the
address of the char[n] array is transferred back. Unfortunately for
you, char w[20] in this context is a local variable of automatic
duration, so its address is not valid after the return of the routine
it is created in. You are thus attempting to employ undefined
behaviour.
--
Prototypes are supertypes of their clones. -- maplesoft
 
Reply With Quote
 
 
 
 
jigsaw
Guest
Posts: n/a
 
      01-16-2006
Got it. Thank you Roberson.

Thanx and
Regards,

jigsaw

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
atoi() Christopher Benson-Manica C++ 16 01-18-2004 11:28 PM
atoi: stringstream or old C sprintf function Mike Chirico C++ 2 11-19-2003 03:59 PM
java equivalent function of c atoi() function lonelyplanet999 Java 8 11-03-2003 01:07 AM
int to char[]: What's the reciprocal of std::atoi? Magig Boatman C++ 1 07-11-2003 07:00 PM



Advertisments