0

Im trying to convert a double to a char* but so far the code I have tried (included below) just results in a segmentation fault. Any help or suggestions would be greatly appreciated!

int main(){

    double ans = 3.41;
    char* ansChar; 
    
    sprintf(ansChar,"%f",ans);
    printf("%s",ansChar);

    return 0;
}
Oblisant
  • 1
  • 2
  • You must allocate buffer before writing. – MikeCAT Nov 22 '20 at 13:26
  • `ansChar` needs to be an *array* of char large enough to hold the string representation of the value (including the string terminator). A pointer doesn’t store a string, it stores the address of the buffer that stores the string. – John Bode Nov 22 '20 at 14:07

0 Answers0