I'm very sorry for making the 1100th thread about this subject. I've been searching all afternoon, scrolling through the questions and solutions but I can't get ANY of 'em to fix my problem.
What I'm trying to do is sending the line generated by the floating_point() function, for example:
166 155 68 60
(when 0.012 is entered)
...back to the main, where the function is called. I've read all about having to use malloc() because the local string is removed as soon as floating_point returns and etc. but my code won't compile.
I might be COMPLETELY wrong in the code below, it's just copy-paste-combine-alter-change of all the solutions I've seen.
Code:/************************ Floating point converter ****************************/ //* Function: converts a decimal value to a floating point //* Parameters: none //* Returns: none //* char floating_point(float flt) { char hex[8], hex_2[8], hex_3[8], temp[3], *p; char **formula; int decimal[4], i=0; short cnt=0, cnt2=0; formula = malloc(20 * sizeof *formula); if ( formula != NULL ) { for ( i = 0; i < 20; ++i ) { formula[i] = malloc(20 * sizeof *formula[i]); if ( formula[i] == NULL ) { for ( i = 0; i < 20; ++i ) { free(formula[i]); } free(formula); return 0; } } } union { unsigned long i; float f; }v; 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++; } sprintf(*formula,"%d\t%d\t%d\t%d",decimal[0],decimal[1],decimal[2],decimal[3]); puts(*formula); return **formula; } /******************************************************************************/
Edit: http://cboard.cprogramming.com/showp...70&postcount=8 for further explanation.Code:int main(void) { char *string; /*code*/ case '7': *string = floating_point(0.012); puts(string); free(string); break; /*code*/ }
I'd actually appreciate someone kicking my butt - and telling me what I'm doing wrong.
Thanks in advance,
René