an elementary question concerning double indirection
Robert G Kaires
kaires at hubcap.clemson.edu
Sun Feb 25 16:07:37 AEST 1990
Dear C users:
Thanks to all who answered my question entitled "A very elementary
question concerning indirection". Your answers were most helpful (and
plentiful). Sorry I could answer each response individually.
This next question is maybe not so elementary (hence I left off the
"very"). I am trying to write a code fragment which gets a floating
point number from the user and checks to see if it contains any illegal
characters. The atof() function can change the input string to a
floating point number and stops converting at the first illegal
character. Searching through my Turbo C manual (2.0), I find the
function strtod() which seems to be better suited for my purpose. This
function not only stops at the first illegal character but also returns
a pointer (or is it a pointer to a pointer?). I don't really understand
why 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:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
char string[30]={""};
char **ptr;
double ans;
clrscr();
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 (without too
much scolding PLEASE, I am still a beginner).
Thanks one and all.
Bob Kaires
More information about the Comp.lang.c
mailing list