Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

atoi crashes program

467 views
Skip to first unread message

JC

unread,
Mar 14, 1999, 3:00:00 AM3/14/99
to
Hi,
I've been trying to write a small parser type routine, unfortunetly it seems
to crash when it performs atoi.

while((inputstring = fgetc(in))!= EOF){
if (inputstring !='P'){
if (inputstring != ' '){
inNum = atoi(inputstring);
number=(number * 10) + inNum;
}
}
}

inNum is declared as an int so I have no idea why it bombs...:(((
Anyone have any ideas???
Thanx
Jon


--
comp.lang.c.moderated - cl...@plethora.net

Paul Lutus

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
The atoi() function requires a char array terminated with a '\0', what is
commonly called a "string" in C. You are providing it with a single
character.

If you say --

char c = '9';

int x = atoi(&c);

-- your program will crash. If you say --

char *str = "9";

int x = atoi(str);

-- the program will work. Your code is like the first example.

--

Paul Lutus
www.arachnoid.com

JC wrote in message ...

<snip>

--
comp.lang.c.moderated - cl...@plethora.net

Jens Schweikhardt

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
JC <ne...@freeuk.com> wrote:
# Hi,
# I've been trying to write a small parser type routine, unfortunetly it seems
# to crash when it performs atoi.

# while((inputstring = fgetc(in))!= EOF){
# if (inputstring !='P'){
# if (inputstring != ' '){
# inNum = atoi(inputstring);
# number=(number * 10) + inNum;
# }
# }
# }

# inNum is declared as an int so I have no idea why it bombs...:(((

You haven't shown us everything, especially the declaration of
inputstring is missing. If it is an int, then you can't pass it as
atoi(inputstring) [probably the reason why it crashes]. If it is a char*
then you should have gotten a bunch of warnings from your compiler
because of conversions of int<->pointer without casts.

Regards,

Jens
--
Jens Schweikhardt http://www.shuttle.de/schweikh/
SIGSIG -- signature too long (core dumped)
--
comp.lang.c.moderated - cl...@plethora.net

Ben Pfaff

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
"JC" <ne...@freeuk.com> writes:

while((inputstring = fgetc(in))!= EOF){

fgetc() returns an int, and you assign it to inputstring.

if (inputstring !='P'){

if (inputstring != ' '){
inNum = atoi(inputstring);

You then try to apply atoi to an int, when it expects a char *.
Blammo!

number=(number * 10) + inNum;
}
}
}

Moral: always include the proper header file. If you'd include
stdlib.h and stdio.h above, you couldn't have the problem that you do.
--
comp.lang.c.moderated - cl...@plethora.net

Juergen Heinzl

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
In article <clcm-1999...@plethora.net>, JC wrote:
>Hi,

>I've been trying to write a small parser type routine, unfortunetly it seems
>to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){

> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);

I do not know how your inputstring looks like, but fgetc() returns an int,
while atoi() expects a char * to a null terminated string. I wonder your
compiler why your compiler does not bail out here.
[...]


>inNum is declared as an int so I have no idea why it bombs...:(((

... programmers fate 8)

Cheers,
Juergen

--
\ Real name : Jürgen Heinzl \ no flames /
\ EMail Private : jue...@monocerus.demon.co.uk \ send money instead /
\ Phone Private : +44 181-332 0750 \ /
--
comp.lang.c.moderated - cl...@plethora.net

Dave Koran

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
JC wrote:
>
> Hi,
> I've been trying to write a small parser type routine, unfortunetly it seems
> to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);

atoi (declared in stdlib.h) has the prototype:

int atoi(const char *nptr);

So what are you doing passing "inputstring" to it?
Every other statement/expression in which you use
it indicates that it's of integral type.

Here's an alternative way to get what you seem to want...

#include <ctype.h> /* for isdigit */

int chargot;

...

while((chargot = fgetc(in))!= EOF){

...

if (isdigit(chargot)){
number=(number * 10) + chargot - '0';
}

...

}

Some folks like to define a macro such as:

#define char2num(x) ((x) - '0')

and then write:

number=(number * 10) + char2num(chargot);

in doing this kind of numeric accumulation
from text input.

Most likely the reason why you can even compile and
link the code you posted is that you are not #include'ing
stdlib.h. If you really want to use atoi, #include
stdlib.h it so the compiler doesn't make erroneous
assumptions about atoi; then pass it a pointer to a
proper C string.

> number=(number * 10) + inNum;
> }
> }
> }
>

> inNum is declared as an int so I have no idea why it bombs...:(((

> Anyone have any ideas???
> Thanx
> Jon
>
> --
> comp.lang.c.moderated - cl...@plethora.net

--
Dave Koran da...@blarg.net
--
comp.lang.c.moderated - cl...@plethora.net

Jerry Coffin

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
In article <clcm-1999...@plethora.net>, ne...@freeuk.com says...

> Hi,
> I've been trying to write a small parser type routine, unfortunetly it seems
> to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){

As an aside, these can be coalesced together:

if (inputstring != 'P' && inputstring != ' ') {

> inNum = atoi(inputstring);


> number=(number * 10) + inNum;

atoi attempts to read in a complete number. Your next line looks like
you're expecting to work with a single digit at a time, and put the
digits together into a number on your own.

For this to compile as-is, it appears that you're not including a
prototype for atoi anywhere. Otherwise, the compiler would warn you
that atoi expects to receive a `char const *' but you're passing it a
char, or (hopefully) an int. It probably doesn't help that you're
using the name `inputstring' even though you're really only reading in
a single character rather than a string.

In this situation, including a prototype for atoi isn't really much of
a cure, because in reality you don't need atoi at all. You probably
want something more like this:

while ((ch=fgetc(in)) != EOF)
if ( ch != 'P' && ch != ' ') {
inNum = ch - '0';
number = number * 10 + inNum;
}

or perhaps:

if (ch != 'P' && ch != ' ' )
number = number * 10 + (ch-'0');
--
comp.lang.c.moderated - cl...@plethora.net

J. Holder

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
JC (ne...@freeuk.com) wrote:
> Hi,
> I've been trying to write a small parser type routine, unfortunetly it seems
> to crash when it performs atoi.

> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){

> inNum = atoi(inputstring);
> number=(number * 10) + inNum;
> }
> }
> }

inputstring is being assigned to a character value returned by fgetc().

You can't pass a char to a routine (atoi) that expects a char*.

If inputstring _is_ defined to be a char*, then the string is not
null-terminated when assigned to the return value of fgetc (since fgetc
didn't return a sting to start with) , and atoi will go running across
memory until something bad happens.

--
John Holder (jho...@frii.com) http://www.frii.com/~jholder/
--
comp.lang.c.moderated - cl...@plethora.net

Lawrence Kirby

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to

>Hi,
>I've been trying to write a small parser type routine, unfortunetly it seems
>to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);
> number=(number * 10) + inNum;
> }
> }
> }

If this compiles you've probably forgotten to #include <stdlib.h> which is
where atoi() is declared.

Where is your definition of inputstring? What type is it? Since you assign
the return value of fgetc() to it it presumably has some integer type,
maybe char, hopefully int (it needs to be int for the test against EOF
to work properly). However atoi() requires that you pass it a pointer to char
which points at the first character of a valid string. That is clearly
incompatible with the previous use of input string.

>inNum is declared as an int so I have no idea why it bombs...:(((
>Anyone have any ideas???

You need to exdplain excactly what it is you are trying to do. What is the
form of the input you are trying to convert? Is it a single digit, a
(possibly multidigit) number?

I suspect that the name ``inputstring'' is causing confusion. My guess is
that the code is trying to read a number digit by digit and build the
resulting value into number. In that case inputstring is not a string at all
it is just a single character. Try calling it something simple like ch and
the problem may become clearer. Also consider that if ch contains a
character from '0' to '9' then you can obtain the corresponding integer
value 0-9 by simply using the expression ch-'0'.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

--
comp.lang.c.moderated - cl...@plethora.net

Mark Brader

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
> while((inputstring = fgetc(in))!= EOF){

The variable is called "inputstring", but this is seriously misleading,
since it contains a single character. It is essential to understand
the difference between characters and strings when programming in C.

> inNum = atoi(inputstring);

However, atoi() actually expects a string, i.e. pointer to the first
character of a string. To convert a single character which is a digit
to its equivalent numeric value, just subtract the character '0' from it.
Retaining the silly name "inputstring", this means:

inNum = inputstring - '0';

Note that if the character is anything OTHER than a digit, this will
produce garbage. See below.
--
Mark Brader, Toronto \ "A good programmer is someone who looks both ways
msbr...@interlog.com \ before crossing a one-way street." -- Doug Linder

My text in this article is in the public domain.
--
comp.lang.c.moderated - cl...@plethora.net

dan

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
I'm going out on a limb with this, but is "inputstring" declared as an int or
char?

Also, where's the rest of your code? If it's epic in nature, perhaps a link to
the code would be in order. And how do you know it's atoi? Done any output
testing (printf's) to see if the value comes back wrong, or does your program
even compile?

I'm still learning, so I may not have the answer for you. However, I have been
exposed to atoi() and parsing.
--Dan

JC wrote:

> Hi,
> I've been trying to write a small parser type routine, unfortunetly it seems
> to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);
> number=(number * 10) + inNum;
> }
> }
> }
>

> inNum is declared as an int so I have no idea why it bombs...:(((
> Anyone have any ideas???

> Thanx
> Jon
>
> --
> comp.lang.c.moderated - cl...@plethora.net

--
comp.lang.c.moderated - cl...@plethora.net

Paul-Marc Marot

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to

JC a écrit dans le message ...

>Hi,
>I've been trying to write a small parser type routine, unfortunetly it
seems
>to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);
> number=(number * 10) + inNum;
> }
> }
> }
>
>inNum is declared as an int so I have no idea why it bombs...:(((
>Anyone have any ideas???
>Thanx
>Jon
>
>
>--
>comp.lang.c.moderated - cl...@plethora.net


How do you declare inputstring?

fgetc returns an int.

atoi wants a char * as input.

Paul-Marc
----------------------------------------------
"Once you discard the impossible, whatever remains, however improbable
, must be the truth."
-- Sir Arthur Conan Doyle


--
comp.lang.c.moderated - cl...@plethora.net

Yuri Sinitski

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
/********************************************

Atoi needs a string, not char as its input character. In fact, you
don’t
need this conversin

Please try this:

******************************************/
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *in;
int inNum;
int number;
int inputstring;

in = fopen ("atoi.c", "r");
if (in == NULL)
{
fprintf (stderr, "\File not found");
exit (1);
}

number = 0;
inputstring = 0;

while((inputstring = fgetc(in))!= EOF)
{
if (inputstring !='P')
{
if (inputstring != ' ')
{

// inNum = atoi(inputstring); Wrong parameter
inNum = inputstring;


number=(number * 10) + inNum;

printf ("%d", number); // debug

}
}
}
}


--
comp.lang.c.moderated - cl...@plethora.net

cbfal...@my-dejanews.com

unread,
Mar 16, 1999, 3:00:00 AM3/16/99
to
In article <clcm-1999...@plethora.net>,

"JC" <ne...@freeuk.com> wrote:
> Hi,
> I've been trying to write a small parser type routine, unfortunetly it seems
> to crash when it performs atoi.
>
> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);

> number=(number * 10) + inNum;
> }
> }
> }
>
> inNum is declared as an int so I have no idea why it bombs...:(((
> Anyone have any ideas???
> Thanx
> Jon
>

You didn't show declarations, but in context inputstring is a char, not a
string. atoi() is expecting a string. Replace the if (input... area with
something like
checkinputchar in range '0' .. '9';
(other chars are termination conditions)
subtract '0' from it
incorporate the result in number as you did above.
> --
> comp.lang.c.moderated - cl...@plethora.net
>

Chuck Falconer (Charles_...@NOSPAMapsnet.com)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
--
comp.lang.c.moderated - cl...@plethora.net

Joyce

unread,
Mar 16, 1999, 3:00:00 AM3/16/99
to
On Sun, 14 Mar 1999 07:07:56 GMT, "JC" <ne...@freeuk.com> wrote:

> while((inputstring = fgetc(in))!= EOF){
> if (inputstring !='P'){
> if (inputstring != ' '){
> inNum = atoi(inputstring);
> number=(number * 10) + inNum;
> }
> }
> }
>
>inNum is declared as an int so I have no idea why it bombs...:(((
>Anyone have any ideas???

How is "inputstring" is declared ??? Because you wrote it's a string.
I think it should be a character or you'd change the code.

Does it compile or not ??
Where does the compiler return an error ???
Otherwise where does it bomb ??

Joyce
ICQ 10607234
Per una pera, una pera cazzo...
Per affrontare questa lunga giornata dura !
--
comp.lang.c.moderated - cl...@plethora.net

0 new messages