Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!asuvax!noao!amethyst!raw From: raw@math.arizona.edu (Rich Walters) Newsgroups: comp.lang.c Subject: Re: an elementary question concerning double indirection Keywords: double indirection, strtod Message-ID: <1458@amethyst.math.arizona.edu> Date: 27 Feb 90 02:47:06 GMT References: <8146@hubcap.clemson.edu> Sender: news@amethyst.math.arizona.edu Organization: Dept. of Math., Univ. of Arizona, Tucson AZ 85721 Lines: 72 In article <8146@hubcap.clemson.edu> kaires@hubcap.clemson.edu (Robert G Kaires) writes: > this function has the syntax: > double strtod(const char *s, char **endptr); > >Why is endptr a pointer to a pointer (and not just a pointer)? >Unfortunately the description of double indirection is poorly explained >in my manuals, and no description of how to use this function is given. >However, I plunged ahead and tried to use it anyway. Here is my short >program: > >char string[30]={""}; >char **ptr; >double ans; > >while(1) > { > gets(string); > if (*string == 'q') break; > ans=strtod(string,ptr); > if ( ( string+strlen(string) ) != *ptr ) /* <--- warning here */ > printf("format error\n"); > else > printf("You typed the number: %f\n",ans); > } >} >This program seems to work OK (try it). However I do get warnings on the >line indicated that "I am trying to use ptr before it is defined". Even >worst, when I use this fragment in a much larger program, all hell >breaks loose. It seems that I am writing over sections of memory that >belong to other varaiables. Please tell me what is wrong. The reason you get the warning is obvious. ptr has not been (explicity) assigned a value. You will get this warning whenever a variable is initialized in a standard function. A better way might be #include /* other appropriate includes */ main() { char string[30]; char *ptr; double ans; extern double strtod(); while(1) { gets(string); if (*string == 'q') break; ans=strtod(string,&ptr); if ( ( string+strlen(string) ) != ptr ) /* <--- warning here */ printf("format error\n"); else printf("You typed the number: %f\n",ans); } } This works. strtod wants to modify ptr to point to the correct place. thus you must send it the address of (a pointer to) the pointer that is to point to the terminating character. On my machine if I send it 12.34abcde, ptr points to the 'b'. I'm not sure why. Richard Walter ------------------------------------------------------------------------------- Keep on crunching those numbers -------------------------------------------------------------------------------