• 10
• 10
• 12
• 14
• 15

# double* to void*? Doesn't seem to work?

This topic is 4219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Trying to cast from a double to a void*. My code works for int, but I can't get the double cast to fully work. Any ideas would be awesome.
//void** d is a parm to this function

double* dblData = (double*)malloc(sizeof(double));
void* varData=NULL;

*dblData = getDblValue(); //set dblData

varData = malloc(sizeof(double));

*((double*)varData) = *dblData;
*d = varData;


] So if you sub double with int, this works. So any idea what i'm doing wrong? Cheers [Edited by - _Sigma on October 5, 2006 9:17:21 PM]

##### Share on other sites
Quote:
 Original post by _SigmaTrying to cast from a double to a void*. My code works for int, but I can't get the double cast to fully work.

Define "doesn't fully work". Do you get a compiler error? Is it a run-time problem?

##### Share on other sites
My appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).

##### Share on other sites
I've tried to incorporate your code into a small test program.
*d = varData;
This line will not compile for me - you are dereferencing a "void *" pointer.

##### Share on other sites
I made a small mistake, d is a void**. Does this change anything? =)Sorry!

##### Share on other sites
So, how are you using this? This prints 6.7 correctly for me:

#include <stdio.h>double getDblValue(){	return 6.7;}void TestFunc(void ** d){	double* dblData = (double*)malloc(sizeof(double));	void* varData=NULL;	*dblData = getDblValue(); //set dblData	varData = malloc(sizeof(double));	*((double*)varData) = *dblData;	*d = varData;}int main(){	void * pd;	TestFunc(&pd);	printf("%f\n", *(double *)pd);	return 0;}

EDIT: Probably the more important question is "what are you actually trying to acheive here?". This code seems very convoluted.

##### Share on other sites
Ooh nice! Thanks a ton! Seems to work fine now. I'd r++ you, but with your 1337 rating, i think that would be more of an insult!

Cheers m8

##### Share on other sites
Quote:
 Original post by _SigmaMy appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).
maybe its not a bug in the above code, but a bug in the way you're printing out the value?
I've found myself stuck on a bug for ages wondering WhyTF is this value garbage!!, oh wait, im using %d instead of %f...

##### Share on other sites
Quote:
Original post by PhilMorton
Quote:
 Original post by _SigmaMy appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).
maybe its not a bug in the above code, but a bug in the way you're printing out the value?
I've found myself stuck on a bug for ages wondering WhyTF is this value garbage!!, oh wait, im using %d instead of %f...

Well it was that too :P But there was indeed a bug in the above code. =)

##### Share on other sites
Quote:
 Original post by _SigmaOoh nice! Thanks a ton! Seems to work fine now. I'd r++ you, but with your 1337 rating, i think that would be more of an insult!Cheers m8

OK, so the maximum rating you can have on these forums is 1337? Interesting... I was hoping to get above 2K someday, but I guess that would't be 1337. Is there some kind of 00ber1337 rating?