Several things bad here. For starters, when posting source code, please use [CODE] tags to preserve formatting and prevent whitespace folding.
Next, you have used str1
as an array, but declared it as a pointer to char. If you are going to store something at the place that str1
points, then str1
should have been initialized to point at some writeable memory (try malloc), or should be defined as an array.
You convert strings to doubles with atof()
, but then try to print them as integers.
Your loop terminates at the end of the literal string, but you only do a conversion to floating point when you find a '+' or '-'. This would explain your 'last number' question.
I assume that after all these years, someone else would have noticed if atof()
really didn't work, so it is probably your expectation of what it should do that is incorrect. What exactly do you mean by "doesn't work"?
Since you seem to know a priori
that your string contains exactly 8 numbers, perhaps it would be easier to use sscanf()
to do your conversions. This would reduce your conversion code to one line:
sscanf( strtemp, "%lf%lf%lf%lf%lf%lf%lf%lf", &temp,&temp,&temp,&temp,&temp,&temp,&temp,&temp );