C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2009, 08:45 PM   #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;			
}
/* -----------------------------------------------------------------------------------------------*/
smjepsen is offline   Reply With Quote
Old 05-25-2009, 09:19 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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***?
tabstop is offline   Reply With Quote
Old 05-25-2009, 09:23 PM   #3
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
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".
cas is offline   Reply With Quote
Old 05-25-2009, 10:48 PM   #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);
smjepsen is offline   Reply With Quote
Reply

Tags
code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help... darkconvoy C Programming 32 04-29-2008 03:33 PM
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
String to Double function jaro C Programming 4 05-27-2006 11:10 AM
Calculator + LinkedList maro009 C++ Programming 20 05-17-2005 12:56 PM
double to string conversion tetriswhiz C Programming 5 04-25-2003 11:41 AM


All times are GMT -6. The time now is 04:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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