What is wrong with this code?

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? :0

char* cmd1;
   strcpy(cmd1, cmd);

Dangerous.
Don't do it.

Even if I don't, still not working )

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.

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?

Raptor5:
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.

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

Raptor5:
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];

Ok. Now how do I pass this to strsep(char sp, char) ?
How do I convert char array to char
* ?

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.

Yes, but compiler stops me from doing like

strsep (&data, "M");

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");

Thanks a lot, guys!