Originally Posted by
citizen
I simply do not understand what is going on because a floating point number is a floating point number. Are you trying to convert a number to string representation? Use snprintf with an empty string then and keep it simple
Originally Posted by
Dave_Sinkula
To me, this sounds like you had trashed your dynamic memory elsewhere in the code you didn't post, and you only discover this when you try a subsequent attempt at further dynamic allocation.
Here's the complete code of the function to make clear what I want it to do:
Code:
/******************************************************************************/
char* floating_point(float flt)
{
char hex[9], hex_2[9], temp[3], *p;
char *formula;
int decimal[4], i=0, ii=0;
short cnt=0, cnt2=0;
union
{
unsigned long i;
float f;
}v;
if(!(formula = malloc(21)))
{
free(formula);//free up resources
if(!(formula = malloc(21)))
exit(-1);
}
v.f = flt;
sprintf(hex_2,"%08lx",v.i);
puts(hex_2);
cnt=7;
while(cnt2<=3)
{
sprintf(temp,"%c%c",hex_2[cnt-1],hex_2[cnt]);
printf("%s\t",temp);
decimal[cnt2] = strtol(temp,&p,16);
cnt-=2;
cnt2++;
}
printf("\n");
sprintf(formula,"%d\t%d\t%d\t%d",decimal[0],decimal[1],decimal[2],decimal[3]);
puts(formula);
return formula;
}
/******************************************************************************/
I hope this helps, as the problem still occurs...