Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to split a string ?

Status
Not open for further replies.

baileychic

Advanced Member level 3
Joined
Aug 2, 2017
Messages
728
Helped
56
Reputation
112
Reaction score
57
Trophy points
28
Activity points
7,033
How to split string according to delimiter ?

I made this code but it gives error on line 1 and line 5.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*char[] StrSplit(char *str, char *_delim) {
    char *_inbuf;
    char *_outstr[320]
 
    strcpy(_inbuf, str);
    
    while(*_inbuf != '$') {
       while(*_inbuf != '\r') {
           *_outstr = strtok(*_inbuf, _delim);
           *_outstr++;
       }
       
       _inbuf++;
    }
    
    return _outstr;
}

 

This function has no sense. *_inbuf is an not defined pointer which by default goes to 0.
If you want to return back the pointer to pointer, you have to allocate them first. I don't see that you are using dinamic allocation like malloc function. So, this function have to include some static buffers to return the pointer address to them which is also has no sense.

- - - Updated - - -

This is my example of splitting the string by the corresponding character:
Code:
char GetParams (char * Source, char ** Dist, char BreakChar, 
                void * memalloc (char len), void * memrelease (char * p))
{
    char i, j, k = 0;
    char * ParamsSizes;

    //get number of params in string separeted by BreakChar
    char n = GetParamsNum(Source, BreakChar);
    
    //reserve memory for pointers
    ParamsSizes = memalloc (n);
    GetParamsSize(Source, ParamsSizes, BreakChar);

    * Dist = memalloc (n);

    for (i = 0; i < n; i ++)
    {
        Dist[i] = memalloc (ParamsSizes[i] + 1);
        
        for (j = 0; j < ParamsSizes[i]; j++)
            Dist[i][j] = Source[k++];
        k++;
        
        Dist[i][j + 1] = 0;
    }
    
    memrelease(ParamsSizes);
    
    return n;
}

char GetParamsNum (char * Source, char BreakChar)
{
    char n = 1;
    while (* Source)
        if (* Source++ == BreakChar)  n++;  
    return n;
}

char GetParamsSize (char * Source, char * Dist, char BreakChar)
{
    char i, n = 0;
    while (* Source)
    {
        i = 0;
        while (* Source++ != BreakChar && * Source) i++;
        if (* Source) 
            Dist[n] = i;
        else 
            Dist[n] = i + 1;
        n++;
    }
    return n;
}

void ReleaseMemory (char ParamNum, char ** Dist, void * memrelease (char * p))
{
  while(ParamNum)
    memrelease (Dist[ParamNum--]);

  memrelease (* Dist);
}
 

Hi

Thanks for replying. This is my new code and it is giving error when debugging. It is not working. It is giving "cannot write to flash" error om line 14 while debugging.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
har *outstr[32];
char *instr = "XXXX,A,1,0,02,01,00,00\rYYYY,A,2,0,03,01,00,00\rZZZZ,A,3,0,04,01,00,00\rUUUU,A,4,0,03,01,00,00\r";
 
void Split_Str(char *_buf, char *_outbuf[], char *_delim) {
   char *token;
   char _inbuf[128];
   int idx = 0;
 
   strcpy(_inbuf, &_buf);
 
   token = strtok (_inbuf,_delim);
 
   while((token != 0) && (idx < 256)) {
      *_outbuf[idx] = *token;
      token = strtok(0, _delim);
      idx++;
   }
}
 
Split_Str(&instr, &outstr, ",");



It is giving "write to FLASH ignored" error on line 14 when debugging. I am using STM32F.
 
Last edited:

@EasyRider

Can you modify my code to use malloc() ? I tried but error gone but code is not working.

Here is my new code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char *outstr[32] = "          ";
char instr[] = "XXXX,A,1,0,02,01,00,00\rYYYY,A,2,0,03,01,00,00\rZZZZ,A,3,0,04,01,00,00\rUUUU,A,4,0,03,01,00,00\r";
char _inbuf[128];
 
void Split_Str(char *_buf, char *_outbuf[], char *_delim) {
   char *token;
   int idx = 0;
   char *ptr2_inbuf;
 
   ptr2_inbuf = (char *)Malloc(sizeof(_inbuf));
   strcpy(*ptr2_inbuf, _buf);
   
   token = strtok (_inbuf, _delim);
   
   while((token != 0) && (idx < 256)) {
      *_outbuf[idx] = *token;
      token = strtok(0, &_delim);
      idx++;
   }
}



I made some changes after refreshing my knowledge about advanced pointers.

Here is my code. It is still giving error and I don't know why.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char *outstr[32] = "          ";
char instr[] = "XXXX,A,1,0,02,01,00,00\rYYYY,A,2,0,03,01,00,00\rZZZZ,A,3,0,04,01,00,00\rUUUU,A,4,0,03,01,00,00\r";
char _inbuf[128];
 
void Split_Str(char *_buf, char *_outbuf[], char *_delim) {
   char *token;
   int idx = 0;
   char *ptr2_inbuf;
 
   ptr2_inbuf = (char *)Malloc(sizeof(_inbuf));
   strcpy(_inbuf, _buf);
   
   token = strtok (_inbuf, _delim);
   
   while((token != 0) && (idx < 256)) {
      _outbuf[idx++] = *token;
      token = strtok(0, &_delim);
   }
}



I see strings being copied to outstr[] but loop executes only two times and then gives error.
 
Last edited:

Try to read pointer value
char * somepointer = malloc (10);
Check the pointer value. In which part of memory it is allocated.
 

I added the malloc pointer to watch window but it shows it is inaccessible.

This is my new code.

If I execute this with delimiter "\r" then it copies to outstr[] only XYZU and then comes out of the loop.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char *outstr[32] = "                              ";
char instr[] = "XXXX,A,1,0,02,01,00,00\rYYYY,A,2,0,03,01,00,00\rZZZZ,A,3,0,04,01,00,00\rUUUU,A,4,0,03,01,00,00\r";
 
void Split_Str(char *_buf, char *_outbuf[], char *_delim) {
   char *token;
   int idx = 0;
   char _inbuf[128];
   char *ptr2_inbuf;
 
   ptr2_inbuf = (char *)Malloc(sizeof(_inbuf));
   strcpy(_inbuf, _buf);
 
   token = strtok (_inbuf, _delim);
 
   while((token != 0) && (idx < 128)) {
      _outbuf[idx] = *token;
      token = strtok(0, _delim);
      idx++;
   }
}



@EasyRider

See picture. Tell me how to use malloc().

I read the pointer value.
 

Attachments

  • malloc test.png
    malloc test.png
    124.3 KB · Views: 128
Last edited:

Dude, forget about the rest code. Delete it temporary. First of all you need make dinamic memory allocation working. If you can't, use static allocation. But don't use functions which is using it. To split the string you only need to return back the size of both strings from your imput buffer. Nothing else.
 

My previous post got updated with picture. Check it and reply.
 

Ok, 0x200000xx - is your ram I belive. So allocation is working. Then copy pasted code also should work. You should run strtok function until it will return null value and you will get the array of pointers to the address of each strings.
 

Maybe it is a bug of mikroC PRO ARM's string library. strtok() not working properly. I have spent 4 days on this.
 

I thinkg they not developed it. It is standart C library. Should work anywhere.
I don't use MikroC for a long time. No idea.
 

Your NMEA-like sentence string suggests that you are just trying to decode the GPS positioning information contained therein, so that the split process itself, although being the core of this thread, a priori would not be strictly necessary, because you can extract the information directly from the buffer itself instead of dealing with creation of temporary variables. I would recommend rethinking if it is worth to complicate the code as you did using pointers over pointers.
 

@andre_teprom

I am not parsing/extracting GPS data.

It is for contolling a device. The time data like PORT,PIN,STATUS,HH,MM,SS,Milli_Sec\r for diffenent pins of MCU are stored in SD Card. The STM32F reads the sd card and then has to extract different fields and act accordingly.

This is my new code and it works fine. I have a new issue related to extracting data from and into a 7 dimensional array. I will ask it tomorrow.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void splitstr_a(char *_inbuf, char _outbuf[][28], char *_delim) {
   char *token;
   int idx = 0;
   char _tmpbuf[128];
   
   strcpy(_tmpbuf, _inbuf);
 
   token = strtok (_tmpbuf, _delim);
 
   while((token != 0) && (idx < 128)) {
      sprintf(_outbuf[idx++], token);
      token = strtok(0, _delim);
   }
}




In C what is th rmax size allocated for a structure type variable ?

My array is like this.

Code:
unsigned char split_string_data[80][7][2][2][3][3][3][4];

If I use this in my structure then I get "not sufficient RAM for structure variable"

If I use it outside then code compiles fine.
 
Last edited:

If I use this in my structure then I get "not sufficient RAM for structure variable"

I can't believe you didn't realize that this strange array requires 241 Kbytes of RAM.

Code:
unsigned char split_string_data[80][7][2][2][3][3][3][4]
 

So, I have to change MCU to one having 512 bytes of RAM ?
 

So, I have to change MCU to one having 512 bytes of RAM ?

You are talking about change your MCU to another with 256 Kbytes to split a string having less than 100 bytes. Think before you ask.
 

My data is like this.

XXXXXX,A,1,0,02,01,00,000
YYYYYY,A,1,0,02,01,00,020
ZZZZZZ,A,1,0,02,01,00,022
UUUUUU,A,1,0,02,01,00,456

80 such lines are repeated. I have to extract each comma separated value into a single array.

The above data are strings and extracted data should also be string.

In the above data each line takes only 18 characters excluding commas because commas are not read into array.

So, 80 X 18 = 1440 bytes.

Now, how to declare array so that each comma separated value will be in a seperate dimension of the array ?
 

Now, how to declare array so that each comma separated value will be in a seperate dimension of the array ?

Trying to link this last question in the context of what was presented earlier, what you want is a struct, not a multidimensional array, anyway I doubt you need to spend 1440 bytes to extract from the original string the information you need.
 
Hi

my previous issue go solved. Now I have new issue. I omitted the multidimensional array[] and used struct. Now I am getting new error.

It says Operator (.) not applicable to these operands on all sprintf() calls

This is my code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void splitstr_b(char _inbuf[][26], PORT_CONTROL_TYPE *pct , char *_delim) {
   char *token;
   int idx = 0;
   int idx_outer = 0;
   int idx_inner = 0;
   char _tmpbuf[80][26];
 
   while(_inbuf[idx]) {
     strcpy(_tmpbuf[idx], _inbuf[idx]);
     idx++;
   }
 
   token = strtok (_tmpbuf[idx_outer], _delim);
 
   while(token != 0) {
      sprintf(*pct.str_direction[idx_inner++], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_port[idx_inner++], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_pin[idx_inner++], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_pin_state[idx_inner++], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_hours[idx_inner++] "%s",, token);
      token = strtok(0, _delim);
      sprintf(*pct.str_minutes[idx_inner++], "%s", token);
      token = strtok(0, _delim);
      sprintf(*pct.str_seconds[idx_inner], "%s", token);
      
      token = strtok(0, _delim);
      
      if(idx_inner >= 6)idx_inner = 0;
      
      idx_outer++;
   }
}
 
splitstr_b(&my_port_control.extracted_data[0], &my_port_control, ",");



PHP:
0 122 Compilation Started __Lib_Delays.c
118 123 Compiled Successfully __Lib_Delays.c
66 1164 Variable 'cycles_div_by_10' has been eliminated by optimizer __Lib_Delays.c
87 1164 Variable 'NumberOfCyc' has been eliminated by optimizer __Lib_Delays.c
104 1164 Variable 'NumberOfCyc' has been eliminated by optimizer __Lib_Delays.c
0 122 Compilation Started __lib_gpio.h
814 123 Compiled Successfully __Lib_GPIO_32F10x_Defs.c
0 122 Compilation Started __Lib_MemManager.c
36 1508 Implicit conversion of int to ptr __Lib_MemManager.c
157 1506 Implicit conversion of pointer to int __Lib_MemManager.c
176 1508 Implicit conversion of int to ptr __Lib_MemManager.c
299 123 Compiled Successfully __Lib_MemManager.c
0 122 Compilation Started stdint.h
583 1508 Implicit conversion of int to ptr __Lib_dlmalloc.c
5472 123 Compiled Successfully __Lib_dlmalloc.c
0 1164 Variable '?FLOC____Lib_dlmalloc_internal_malloc_stats?T372' has been eliminated by optimizer __Lib_dlmalloc.c
3612 1164 Variable 'maxfp' has been eliminated by optimizer __Lib_dlmalloc.c
3613 1164 Variable 'fp' has been eliminated by optimizer __Lib_dlmalloc.c
3614 1164 Variable 'used' has been eliminated by optimizer __Lib_dlmalloc.c
3917 1164 Variable 'offset' has been eliminated by optimizer __Lib_dlmalloc.c
3939 1164 Variable 'flags' has been eliminated by optimizer __Lib_dlmalloc.c
3979 1164 Variable 'offset' has been eliminated by optimizer __Lib_dlmalloc.c
3996 1164 Variable 'bin' has been eliminated by optimizer __Lib_dlmalloc.c
4054 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4054 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4054 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4025 1164 Variable 'psize' has been eliminated by optimizer __Lib_dlmalloc.c
4109 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4109 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4109 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4107 1164 Variable 'tn' has been eliminated by optimizer __Lib_dlmalloc.c
4066 1164 Variable 'oldsp' has been eliminated by optimizer __Lib_dlmalloc.c
4070 1164 Variable 'offset' has been eliminated by optimizer __Lib_dlmalloc.c
4075 1164 Variable 'tnext' has been eliminated by optimizer __Lib_dlmalloc.c
4077 1164 Variable 'nfences' has been eliminated by optimizer __Lib_dlmalloc.c
4204 1164 Variable 'end' has been eliminated by optimizer __Lib_dlmalloc.c
4313 1164 Variable 'r' has been eliminated by optimizer __Lib_dlmalloc.c
4361 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4361 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4361 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4332 1164 Variable 'pred' has been eliminated by optimizer __Lib_dlmalloc.c
4394 1164 Variable 'newsize' has been eliminated by optimizer __Lib_dlmalloc.c
4447 1164 Variable 'prev' has been eliminated by optimizer __Lib_dlmalloc.c
4505 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4505 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4505 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4520 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4520 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4520 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4549 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4549 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4549 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4547 1164 Variable 'i' has been eliminated by optimizer __Lib_dlmalloc.c
4548 1164 Variable 'leastbit' has been eliminated by optimizer __Lib_dlmalloc.c
4575 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4575 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4575 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4591 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4591 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4591 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4589 1164 Variable 'i' has been eliminated by optimizer __Lib_dlmalloc.c
4590 1164 Variable 'leastbit' has been eliminated by optimizer __Lib_dlmalloc.c
4683 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4683 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4683 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4681 1164 Variable 'leftbits' has been eliminated by optimizer __Lib_dlmalloc.c
4682 1164 Variable 'leastbit' has been eliminated by optimizer __Lib_dlmalloc.c
4740 1164 Variable 'rsize' has been eliminated by optimizer __Lib_dlmalloc.c
4742 1164 Variable 'r' has been eliminated by optimizer __Lib_dlmalloc.c
4795 1164 Variable 'prev' has been eliminated by optimizer __Lib_dlmalloc.c
4816 1164 Variable 'tsize' has been eliminated by optimizer __Lib_dlmalloc.c
4853 1164 Variable 'Y' has been eliminated by optimizer __Lib_dlmalloc.c
4853 1164 Variable 'N' has been eliminated by optimizer __Lib_dlmalloc.c
4853 1164 Variable 'K' has been eliminated by optimizer __Lib_dlmalloc.c
4914 1164 Variable 'newsize' has been eliminated by optimizer __Lib_dlmalloc.c
5005 1164 Variable 'pos' has been eliminated by optimizer __Lib_dlmalloc.c
4987 1164 Variable 'req' has been eliminated by optimizer __Lib_dlmalloc.c
5125 1164 Variable 'array_chunk_size' has been eliminated by optimizer __Lib_dlmalloc.c
5065 1164 Variable 'array_chunk' has been eliminated by optimizer __Lib_dlmalloc.c
5330 1164 Variable 'newp' has been eliminated by optimizer __Lib_dlmalloc.c
5355 1164 Variable 'r' has been eliminated by optimizer __Lib_dlmalloc.c
5416 1164 Variable 'result' has been eliminated by optimizer __Lib_dlmalloc.c
0 122 Compilation Started __lib_fat32.h
105 123 Compiled Successfully __Lib_FAT32_Defs_STM32_M3_M4_M7.c
0 122 Compilation Started __lib_fat32.h
298 123 Compiled Successfully __Lib_FAT32_Driver_STM32_M3_M4_M7.c
87 1164 Variable 'c_size' has been eliminated by optimizer __Lib_FAT32_Driver_STM32_M3_M4_M7.c
87 1164 Variable 'c_size_mult' has been eliminated by optimizer __Lib_FAT32_Driver_STM32_M3_M4_M7.c
89 1164 Variable 'blocknr' has been eliminated by optimizer __Lib_FAT32_Driver_STM32_M3_M4_M7.c
0 122 Compilation Started string.h
0 1004 interrupt handler (Timer4_interrupt at 0x002E) Port_Control.c
0 1004 interrupt handler (usart_RX at 0x0035) Port_Control.c
354 317 Operator '.' is not applicable to these operands 'pct' Port_Control.c
354 317 Operator '' is not applicable to these operands '' Port_Control.c
354 322 Pointer required Port_Control.c
354 322 Pointer required Port_Control.c
354 307 Illegal typecast 'can not convert to pointer' '' Port_Control.c
356 317 Operator '.' is not applicable to these operands 'pct' Port_Control.c
356 317 Operator '' is not applicable to these operands '' Port_Control.c
356 322 Pointer required Port_Control.c
356 322 Pointer required Port_Control.c
356 307 Illegal typecast 'can not convert to pointer' '' Port_Control.c
358 317 Operator '.' is not applicable to these operands 'pct' Port_Control.c
358 317 Operator '' is not applicable to these operands '' Port_Control.c
358 322 Pointer required Port_Control.c
358 322 Pointer required Port_Control.c
358 307 Illegal typecast 'can not convert to pointer' '' Port_Control.c
360 317 Operator '.' is not applicable to these operands 'pct' Port_Control.c
360 317 Operator '' is not applicable to these operands '' Port_Control.c
360 322 Pointer required Port_Control.c
360 322 Pointer required Port_Control.c
360 307 Illegal typecast 'can not convert to pointer' '' Port_Control.c
362 317 Operator '.' is not applicable to these operands 'pct' Port_Control.c
362 317 Operator '' is not applicable to these operands '' Port_Control.c
362 322 Pointer required Port_Control.c
362 322 Pointer required Port_Control.c
362 307 Illegal typecast 'can not convert to pointer' '' Port_Control.c
362 314 Not enough parameters Port_Control.c
362 300 Syntax Error: ')' expected,  but '"%s"' found Port_Control.c
362 315 Invalid expression Port_Control.c
362 402 ; expected, but ')' found Port_Control.c
362 424 '}' expected ';' found Port_Control.c
373 424 '}' expected '}' found Port_Control.c
374 312 Internal error '' Port_Control.c
0 102 Finished (with errors): 07 Sep 2017, 11:38:56 Port_Control.mcpar
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top