Sign in to follow this  
_Sigma

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

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 this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
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.


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
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).
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 this post


Link to post
Share on other sites
Quote:
Original post by PhilMorton
Quote:
Original post by _Sigma
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).
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 this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
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


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?

Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush
OK, so the maximum rating you can have on these forums is 1337?


No, but a rating of 1337 is more leet than a rating of 1338, for example. So, _Sigma did not want to rate me up in order to avoid messing that up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this