Pages: [1]   Go Down
Author Topic: What is wrong with this code?  (Read 416 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley-mad
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even if I don't, still not working )
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2313
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  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 Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Online Online
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   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 Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please, supply an example of how to do this correctly. Thanks.
Logged

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please, supply an example of how to do this correctly. Thanks.

Code:
char data[] = "This data is actually stored somewhere correctly";

or if you don't know what you needs to go into it yet:

Code:
char data[10];
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The same way you did before:

Code:
&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 Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The same way you did before:

Yes, but compiler stops me from doing like

strsep (&data, "M");
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You may have to type cast it:
Code:
strsep ((char**)&data, "M");

or if you don't want to type cast you could do:

Code:
char data[] = "whatever";
char* ptr = data;
strsep(&ptr,"M");
Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot, guys!
Logged

Pages: [1]   Go Up
Jump to: