I've assigned a number into character pointer:
I want to convert pointer strNumSet into float. What is the correct way to do this?Code:char* strNumSet = "12.33";
Kindly thanks for any suggestion and help.
I've assigned a number into character pointer:
I want to convert pointer strNumSet into float. What is the correct way to do this?Code:char* strNumSet = "12.33";
Kindly thanks for any suggestion and help.
Many possible ways. One option involves use of the function sscanf() (note the two s's). Another other involves use of a function named atof().
Of course, if you have done this in code, there is also the question of why you haven't done
although admittedly that does not work for arbitrary strings (eg read from file, accepted from user input).Code:float float_value = 12.33;
Incidentally "12.33" is not a character pointer that contains a number. It is an array of char that contains some characters you happen to interpret as a number. And a pointer is not the same thing as an array, even though (in some contexts) you can use it it was one.
Converting pointer to float.
Don't cross-post
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.