Offline
Newbie
Karma: 0
Posts: 34
|
 |
« on: November 17, 2012, 01:06:24 pm » |
int cmdNumber(char* cmd) { char* cmd1; strcpy(cmd1, cmd); //Serial.println("123"); char* ass = strsep(&cmd1, "M"); ass = strsep(&cmd1, " "); // Serial.println(ass); int aaa = atoi(ass); // Serial.println(aaa); return aaa; } void loop() { int v = cmdNumber("M1001 "); Serial.println(v, DEC); ... I get 0 in my port monitor. If I uncomment Serial.println(aaa) everything goes fine and I get 1001. What the hell is wrong? 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: November 17, 2012, 01:15:59 pm » |
char* cmd1; strcpy(cmd1, cmd); Dangerous. Don't do it.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #2 on: November 17, 2012, 01:20:20 pm » |
Even if I don't, still not working )
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 671
|
 |
« Reply #3 on: November 17, 2012, 01:23:05 pm » |
char* ass = strsep(&cmd1, "M");
If you want to return ass create it on the heap not the stack. The stack may be over written. Or just make it global.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #4 on: November 17, 2012, 01:32:41 pm » |
Even this is not working
void loop() { char* a = "M1001 2334 3434"; char* a1 = strsep(&a, " "); Serial.println(a);
How do I create it on heap?
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1869
|
 |
« Reply #5 on: November 17, 2012, 01:43:03 pm » |
char* a = "M1001 2334 3434";
Stop creating pointers to nothing and assigning it a value. If you want to store a string of chars like that you need to actually set aside space to store those characters by using an array.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #6 on: November 17, 2012, 01:45:08 pm » |
Please, supply an example of how to do this correctly. Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1869
|
 |
« Reply #7 on: November 17, 2012, 01:47:58 pm » |
Please, supply an example of how to do this correctly. Thanks.
char data[] = "This data is actually stored somewhere correctly"; or if you don't know what you needs to go into it yet: char data[10];
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #8 on: November 17, 2012, 01:49:23 pm » |
Ok. Now how do I pass this to strsep(char **sp, char*) ? How do I convert char array to char** ?
|
|
|
|
« Last Edit: November 17, 2012, 02:18:40 pm by Raptor5 »
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #9 on: November 17, 2012, 03:04:00 pm » |
The same way you did before: &data 'data' is just a pointer to the start of an array, and in this case the array is of type char, so 'data' a char* As a side note, when you access an element of an array, you are essentially saying 'get the value stored at the pointer to the start of the array plus some offset'. e.g.: data[0] is the same as *data data[1] is the same as *(data + 1) data[2] is the same as *(data + 2) and so on.
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #10 on: November 17, 2012, 03:11:53 pm » |
The same way you did before:
Yes, but compiler stops me from doing like strsep (&data, "M");
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #11 on: November 17, 2012, 03:40:36 pm » |
You may have to type cast it: strsep ((char**)&data, "M"); or if you don't want to type cast you could do: char data[] = "whatever"; char* ptr = data; strsep(&ptr,"M");
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #12 on: November 17, 2012, 04:33:33 pm » |
Thanks a lot, guys!
|
|
|
|
|
Logged
|
|
|
|
|
|