string to double conversion with strtod

This is a discussion on string to double conversion with strtod within the C Programming forums, part of the General Programming Boards category; Dear C-programming community, I am trying to write a command-line parser that tests for successful conversion from string to double. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Question string to double conversion with strtod

    Dear C-programming community,

    I am trying to write a command-line parser that tests for successful conversion from string to double. I am getting the following warning from my gcc compiler for the code below: "warning: passing argument 2 of ‘strtod’ from incompatible pointer type." I am not sure how serious this warning is. I have been unsuccessful in getting rid of the warnings. Can anyone help me to improve my code and eliminate compiler warnings? I am not the greatest at pointers, so I could be missing something. I hope I followed proper protocol for posting code. Thanks.

    Steve

    Code:
    /* -----------------------------------------------------------------------------------------------*/
    #include <stdlib.h> /* for strtod */
    #include <stdio.h>
    #include <string.h> /* for strcpy */
    
    void parseme( char **s ) {
    	
    	double temp;
    	char **p;
    	int i, x, y;
    
    	for(i = 0; i < 2; i++) {
    
    		printf("\ns from parseme = %s\n", s[i]);
    		temp = strtod(s[i], &p);
    		printf("temp = %f\n", temp);
    
    		printf("address of s[0] = %p\n", s[i]);
    		printf("address of p = %p\n", p);
    	
    		x = (int)p;
    		printf("x = %d\n", x);
    	
    		y = (int)s[i];
    		printf("y = %d\n", y);
    	
    		if(x == y) {
    			printf("conversion failed.\n\n");
    			}
    		else {
    			printf("conversion succeeded.\n\n");
    			}
    	}	
    }
    /* -----------------------------------------------------------------------------------------------*/
    
    /* -----------------------------------------------------------------------------------------------*/
    
    void parseme( char ** );
    
    int main( void ) {
    
    	char *s[2];
    
    	s[0] = "hello";
    	s[1] = "150";
    
    	parseme(s);
    	
    	return 0;			
    }
    /* -----------------------------------------------------------------------------------------------*/

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So have you looked at the man page/textbook bit for strtod? Do you see how it expects a char**? Why then do you pass it a char***?

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    If you have char **p, then &p has type char***. The second argument to strtod() is supposed to be char**. The way it's used is:
    Code:
    char *p;
    strtod(string, &p);
    Now p points to the first character that could not be converted (which might be the null character). If you don't have a use for this, you can just pass NULL as the second argument.

    I would also recommend dropping your casts. When the compiler complains, a cast is usually not the right response. The cast generally doesn't fix the problem, but just tells the compiler to be quiet about it. In your case, you're trying to convert pointers to integers, which is not useful for you.

    You can see good documentation for this function here. This link documents POSIX extensions to the C standard, but they're clearly marked with "CX".

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2
    cas,

    Thanks. I see what you and tabstop were saying. I must have misread the page I googled. When I did the following it worked nicely. Thanks much for your help. I'll continue to resist going back to Fortran for now...

    char *u;
    char *p;
    u = s[0];
    temp = strtod( u, &p);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 11:10 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. double to string conversion
    By tetriswhiz in forum C Programming
    Replies: 5
    Last Post: 04-25-2003, 11:41 AM

Tags for this Thread


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