Nasty bug

e89hse at rigel.efd.lth.se e89hse at rigel.efd.lth.se
Thu Aug 30 12:41:19 AEST 1990


Hi !

 I had a lot of trouble with a bug yesterday. The code was similar to the
following:

void main()
{
    prnval("10.0",0.0);
    exit(0);
}

prnval(s,f)
char   *s;
float   f;
{
   if(f == 0.0)
       sscanf(s,"%f",&f);
   printf("%10.2f\n",f);
}

And it didn't work. Why? The answer is that the parameter f is a
double, not a float since all floats are converted to double when they are
passed as arguments to functions. Therefore &f is a ptr to double rather than a
ptr to float as one would expect looking at the declartion. Later I rewrote
prnval() as:

prnval(s,f)
char *s;
double f;
{
   if(f == 0.0)
       sscanf(s,"%lf",&f);
   printf("%10.2f\n",f);
}

 Well, I hope I'll help someone to avoid that problem... It took me a while to
figure out why the code didn't work...

 Henrik Sandell

Disclaimer: I know that this code wouldn't pass lint without warnings, but that
is not the purpose of the code...



More information about the Comp.lang.c mailing list