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

strtod returns incorrect terminating char pointer when converting "Inf"

6 views
Skip to first unread message

Wan-teh Chang

unread,
May 6, 1995, 3:00:00 AM5/6/95
to

Hi everyone,

I noticed that strtod's terminating char pointer is one char off
when it converts the strings "Inf" and "Infinity" to doubles.
The return values are correct though (the infinity special value).

I am running SunOS 5.4 (Solaris 2.4) on a Sparc 5.
The C compiler (cc) version is SC3.0.1 13 Jul 1994.
I have a short test program test.c below where I first use strtod
to convert a few strings that are ASCII formats of regular doubles,
then I use strtod to convert strings "Inf", "Infi", "Infin", ...,
"Infinity", and some others. I check both the return values and
the terminating char pointer endPtr.

The return values are always correct. The endPtr pointer is correct
for the regular doubles, but is one char off for "Inf" and "Infinity",
(it points at the character after the null byte; it should point at
the null byte), and for some of the other test strings too.

Anyone see this problem with strtod before? Anyone know a workaround?

Thanks,
Wan-teh Chang

******************************* begin file test.c *************************
#include <stdio.h>
#include <string.h>
#include <errno.h>

extern double strtod();

main()
{
double d;
int i;
char *endPtr;
char buf0[128], buf1[128], buf2[128], buf3[128], buf4[128], buf5[128];
char buf6[128], buf7[128], buf8[128], buf9[128], buf10[128], buf11[128];
char buf12[128];
char *string[13];

strcpy(buf0, "234");
strcpy(buf1, "-2.34");
strcpy(buf2, "2.34e23");
strcpy(buf3, "In");
strcpy(buf4, "Inf");
strcpy(buf5, "Infi");
strcpy(buf6, "Infin");
strcpy(buf7, "Infini");
strcpy(buf8, "Infinit");
strcpy(buf9, "Infinity");
strcpy(buf10, "Infiniti");
strcpy(buf11, "Infinitya");
strcpy(buf12, "Infa");
string[0] = buf0;
string[1] = buf1;
string[2] = buf2;
string[3] = buf3;
string[4] = buf4;
string[5] = buf5;
string[6] = buf6;
string[7] = buf7;
string[8] = buf8;
string[9] = buf9;
string[10] = buf10;
string[11] = buf11;
string[12] = buf12;

for (i = 0; i < 13; i++) {
errno = 0;
d = strtod(string[i], &endPtr);
printf("strtod converts string %s to double %g\n", string[i], d);
printf("endPtr is %d chars offset from the head of string\n",
endPtr - string[i]);
if (errno != 0) {
printf("errno == %d\n", errno);
}
printf("\n");
}
}
********************** end file test.c ******************************

********************** begin output of test program *******************

strtod converts string 234 to double 234
endPtr is 3 chars offset from the head of string

strtod converts string -2.34 to double -2.34
endPtr is 5 chars offset from the head of string

strtod converts string 2.34e23 to double 2.34e+23
endPtr is 7 chars offset from the head of string

strtod converts string In to double 0
endPtr is 0 chars offset from the head of string

strtod converts string Inf to double Inf
endPtr is 4 chars offset from the head of string

strtod converts string Infi to double Inf
endPtr is 4 chars offset from the head of string

strtod converts string Infin to double Inf
endPtr is 4 chars offset from the head of string

strtod converts string Infini to double Inf
endPtr is 4 chars offset from the head of string

strtod converts string Infinit to double Inf
endPtr is 4 chars offset from the head of string

strtod converts string Infinity to double Inf
endPtr is 9 chars offset from the head of string

strtod converts string Infiniti to double Inf
endPtr is 3 chars offset from the head of string

strtod converts string Infinitya to double Inf
endPtr is 8 chars offset from the head of string

strtod converts string Infa to double Inf
endPtr is 3 chars offset from the head of string

********************** end output of test program **********************
--
Wan-teh Chang
Graduate student
Department of EECS
University of California

0 new messages