convert character pointer to a float using atof

So i have a string of floats, seperated by semicolons. I have converted that to a character array so that I can use strtok to split them up.
I now want to convert each of those to floats, however I cannot use atof on a character pointer, but according to the documentation I need to store the token positions ina character pointer. It fails on the line

 arr[cnt] = atof(p);

here is the full code:

#include <string.h>
	
void setup()
{
Serial.begin(9600);
string FileMeasure="0.11;1.33;5.22"
int TempNumOne=FileMeasure.size();
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
        {
            Filename[a]=FileMeasure[a];
        }
 char *p = strtok(Filename, ";");
 byte cnt = 0;
 float arr[3];
 While (p) {
 Serial.println(p);
 p = strtok(NULL, ";");
 arr[cnt] = atof(p);
 Serial.println(arr[cnt]);
 cnt++;
 }
 void loop()
 {
 }

Thanks guys

It fails on the line ...

No it doesn't. For one thing there is no semicolon here (at the end):

string FileMeasure="0.11;1.33;5.22"

And "while" is lower-case:

 While (p) {

Did you retype the whole thing? You can copy and paste you know.

Sorry Im at work, I couldnt run it from here but I had an old version in my email. The string coming in is coming from a sensor board - so there is no semicolon at the end.
Does the code work for you? It doesnt for me (at least it didnt last night).
Does your array print then?

It doesn't compile. Look, don't waste our time posting something that is "sort of" like your problem. Save it up until you can post the actual code.

How have I wasted your time? A while loop was capitalized? Surely if your replying at all you can see that the error is to do with me trying to convert the character pointer to a float. I didn't title my thread "While loop is capitalized - what do i do" did I?
The fact that it doesnt compile surely explains my problem with the atof command?
So to save you the trouble of making that adjustment I have re uploaded the code below with the while not capitalized...

while (p) {
 Serial.println(p);
 p = strtok(NULL, ";");
 arr[cnt] = atof(p);
 Serial.println(arr[cnt]);
 cnt++;
 }

So I wonder if you can help me convert p to a float???

a.mlw.walker:
Surely if your replying at all you can see that the error is to do with me trying to convert the character pointer to a float

What error?

Read this:

It wastes our time because there are half a dozen mistakes that need fixing before we can start to actually look at the problem in hand. If you want help, post code that at least compiles. The code you posted doesn't even get past an auto-format.

You are asking for help with code that doesn't compile (but not on the line you are complaining about) and you haven't posted the error message.

That's like going to a garage and saying "can you fix my car?" (but I brought my wife's car instead) and without saying what the problem is.

a.mlw.walker:
So to save you the trouble of making that adjustment ...

It's no trouble, I assure you.

That’s like going to a garage and saying “can you fix my car?” (but I brought my wife’s car instead) and without saying what the problem is.

I think I have stated my problem? I cant convert a character pointer to a float???

No it doesn’t. For one thing there is no semicolon here (at the end):

sorry I misunderstood this line, i thought you meant I needed a semicolon at the end of the string so that strtok would recongize the last value i.e like this

string FileMeasure="0.11;1.33;5.22[b][color=red];[/color][/b]";

but you meant the one on the end…

Forget my code for a minute. I have spent a lot of time looking how to split up a string based on delimiters. I understand that I need to convert it to a character array to do this - which I have done in this bit of code:

for (int a=0;a<=TempNumOne;a++)
        {
            Filename[a]=FileMeasure[a];
        }

And I understand to actually split it up I require (in my case) the pointer p and strtok.
However I understand thata character pointer is just pointing to the memory location of the beginning of each of the values (in my string), so what I dont understand is how to convert the strings stored and begin at each of these locations to floats. I am pretty sure I need atof to do that. This i thought would be a common requirement but it doesnt seem very common on the forums.

Look, I don't want so seem obtuse, but could you do two things for me please?

  • Post your code
  • Post the error message

I hope that's not too much to ask, and then we can move on to solving your problem.

I cant convert a character pointer to a float???

Then, you ARE doing something wrong. The atof() function takes a pointer to char as an input. It is not a coincidence that this is what strtok() returns.

We'll never know, PaulS, because he is just retyping code out of his head rather than posting what he is actually using.

... to save you the trouble of making that adjustment ...

...

... i thought would be a common requirement but it doesnt seem very common on the forums ...

And then goes into "insult the helpers" mode.

… i thought would be a common requirement but it doesnt seem very common on the forums …

Is that an insult? I thought that was a bit of wonderment…

In terms of

… to save you the trouble of making that adjustment …

yeah that was a bit of sarcasm, but you were complaining about a capital ‘W’. If it was wasting your time I would have thought you wouldnt have bothered repyling in the first place. Dont get me wrong forums are the greatest invention ever - it allows people to get help that would otherwise be impossible, but it seemed to me like you were picking on a point that was nothing to do with the question at hand which from my posts was pretty clear. As it turns out I turned to a c++ book and solved it.
This line is splitting off the first float:

 char *p = strtok(Filename, ";");

If you had known that, it would have been easier just to tell me, if you didnt then Im not sure why you joined the conversation?