Converting pointer to float.

This is a discussion on Converting pointer to float. within the C++ Programming forums, part of the General Programming Boards category; I've assigned a number into character pointer: Code: char* strNumSet = "12.33"; I want to convert pointer strNumSet into 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,249
    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%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21