There is a char data type which contains the following text:

+CMT: "+79643437397","","15/06/09,17:34:01+12" 1+ON+60 

It starts with \ r \ n , but here it does not display, because in the buffer from where it turns out there may be other data, it is required to split it by \ r \ n and check the length, if the length is 3 lines, then the last line is necessary split by plus, on JS I would do it like this:

 var a = " +CMT: "+79643437397","","15/06/09,17:34:01+12" 1+ON+60 "; var b = a.split("/r/n"); if (b.length == 3) { var c = b[2].split("+"); if (c.length == 3) { // Выполнение функции по полученным данным из c ({1, "ON", 60}) } } 

But I work with C for the first time, I decided to run an example that I found on the Internet:

 #include <stdio.h> #include <string.h> #include <malloc.h> char str[] = "John|Doe|Melbourne|6270|AU"; char *fname, *lname; char *oldstr = malloc(sizeof(str)); strcpy(oldstr,str); fname=strtok(str,"|"); lname=strtok(NULL,"|"); printf("Firstname: %s\n", fname); printf("Lastname: %s\n", lname); free(oldstr); 

but everything is so smooth here, the malloc.h library was not in the Arduino development environment at all, I downloaded this - http://gee.cs.oswego.edu/pub/misc/malloc.h , but I think it’s obviously that This is not the case; as a result, the error

 Blink.ino: In function 'void setup()': Blink:27: error: invalid conversion from 'void*' to 'char*' [-fpermissive] invalid conversion from 'void*' to 'char*' [-fpermissive] 

Here's what is on line 27:

 char *oldstr = malloc(sizeof(str)); 

As a result, I don’t know what to do with all this, help with an example.

  • one
    char *oldstr = (char *)malloc(sizeof(str)); - user6550
  • @klopp: Why is this all of a sudden? - AnT
  • @GeneralProger: Your compilation errors come up primarily because you are compiling this C code as C ++ code. However, for some reason you put [C] in the tag here. You already decide what language you are trying to use - C or C ++. C and C ++ are two completely different languages. - AnT
  • @AnT, arduino with C has its own troubles, and its own compilers. - user6550
  • @klopp: This is exactly the problem (the ban on the conversion of void * to char * ) in Arduino to C is not. - AnT

1 answer 1

I have already written in the comment that for the problem described by you no split () is needed.

Try (without malloc, processing by source data)

 // Возвращаем позицию первого плюса в 3-ей строке или 0 (нет плюса или не 3 строки // по указателю nlpos вернем позицию последнего \n в str[] int plpos (const char *str, int *nlpos) { int nl = 0, plpos = 0, i; for (*nlpos = -1, i = 0; str[i]; i++) if (str[i] == '\n') { nl++; plpos = 0; *nlpos = i; } else if (str[i] == '+' && !plpos) plpos = i; return nl == 2 ? plpos : 0; } 

and somewhere in your program:

 int nlpos, ppos = plpos(my_str, &nlpos); if (ppos) { char *str3 = my_str + nlpos + 1; // тут обрабатываете свою 3-ю строку (str3) int str3ppos = ppos - nlpos - 1; // это позиция первого плюса "в координатах" str3 ... } 

If the data starting with str3 should actually be a copy , then write:

 .... char str3 = strdup(my_str + nlpos + 1); .... 

In this case, the strdup function (I hope it is in the arduino) will make malloc (then don't forget free).