![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 2
| 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 | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #3 |
| 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); 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 | |
| | #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 | |
![]() |
| Tags |
| code |
| Thread Tools | |
| Display Modes | |
|
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 |