Thread: Converting pointer to float.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Converting pointer to float.

    I've assigned a number into character pointer:

    Code:
    char* strNumSet = "12.33";
    I want to convert pointer strNumSet into float. What is the correct way to do this?

    Kindly thanks for any suggestion and help.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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
    Code:
       float float_value = 12.33;
    although admittedly that does not work for arbitrary strings (eg read from file, accepted from user input).

    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM