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
    Sep 2004
    Location
    California
    Posts
    3,268
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    strtofl():
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, const char *argv[]) {
    	char s[] = "42.7";
    	float n = strtof(s,NULL);
    
    	printf("%s %f\n",s,n);
    
    	return 0;
    }
    The slight imprecision which might be observable here is due to the nature of floating point numbers.

    If you are using gcc, you must compile -std=c99. Otherwise, you can use "string to double", strtod().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  4. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM