Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using strtod with a pointer to a double doesn't work

Status
Not open for further replies.

greadey

Technical User
Oct 25, 2001
231
GB
Hi all,

using the following code;

#include <stdio.h>
#include <stdlib.h>

int main () {

char *val = &quot;1.234&quot;;
double pnum;

pnum = strtod(val, NULL);

printf(&quot;%f\n&quot;, pnum);

return(0);

}

works just fine and dandy however writing the code like this;

#include <stdio.h>
#include <stdlib.h>

int main () {

char *val = &quot;1.234&quot;;

double *pnum;

*pnum = strtod(val, NULL);

printf(&quot;%f\n&quot;, *pnum);

return(0);

}

results in segmentation faults, exceptions and general mayhem.

Any ideas why?

Can you declare a pointer to a double?

greadey.
 
double* pnum;

There is not yet a double, only an uninitialized pointer.

Use:

double* pnum = new double;
Greetings,
Rick
 
Do you mean that there is not yet a double * implemented in the language?
 
Just been looking and found that the declaration;

double *pNum is valid. As far as it not being initialised, why should that matter in the code above. What I have done is declared the space for a pointer to a double with an identifier. the pointer is then assigned to with the strtod function. Quelle problem?
greadey
 
You're not returning the value, you're attempting to set to an address in memory ( sorry, I can't put it better).
strtod expects to return a double, not a pointer to a double
also - in your printf, it should be pnum, not *pnum.
Dickie Bird (:)-)))
 
>> Do you mean that there is not yet a double * implemented
>> in the language?

No, you are wrong.

>> why should that matter

Yes it matters big time! You don't understand pointers.

Code:
double* pnum;

says that the value of the variable pnum contains a memory address. Since you don't initialize it to a valid memory address it contains random data that likely will point to an invalid memory address. Therefore:

Code:
*pnum = strtod(...);

is trying to write to memory at the address value contained in the variable pnum, and since it is invalid it crashes.

-pete
 
You're right I don't fully understand pointers, that's why I write small silly bits of code like this - to see if I DO understand pointers.

Anyway, thanks for all your input I shall continue my quest.
 
I think I'm right in putting it like this:

double *pnum
sets aside space to hold an address, and notes that this will be the address of a double. It doesn't make any space for the double itself, not at all!
Anything starting
*pnum = ....
will put its answer in that address (which is currently just a hypothetical address that hasn't been set to anything).
*pnum = ....
is thus very different to
pnum = ....
(if you go back and look at your two examples, the one that worked, and the one that didn't, you'll see that this is one of the major differences between them).

If you write
double *pnum = new double
what you are doing is setting aside the space for the address, and also initializing it to hold the address of a brand new double. NOW it's safe to put something in that address, because it points to something.

You got a segmentation fault because you tried to write something at a totally random address, so the computer objected that you had no right to charge around in that segment of memory (a random segment).

Hope that helps a bit.
Tell me if that was a load of rubbish, someone.

And don't worry about pointers being tricky to get used to. A lot of my nastiest errors involve type-casts of pointers to pointers to something, and things like that....
 
Thanks Lionelhill,l

That seems to make alot of sense now. I think my biggest problem is muddling perl references with C pointers. W£hat is being said here is that it is not possible to make a pointer / reference to an anonymous value. In simple terms;

double Num;
double *pNum;

pNum = & Num;

would then allow pNUm to take the value returned from the strtod() function.

:))
 
Yes i think you have it, as long as you realize that by doing this:

Code:
*pNum = strtod(...);

you changed the value of &quot;Num&quot;

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top