baileychic
Advanced Member level 3
How to split string according to delimiter ?
I made this code but it gives error on line 1 and line 5.
I made this code but it gives error on line 1 and line 5.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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);
}
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, ",");
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++; } }
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); } }
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++; } }
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); } }
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"
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 ?
Now, how to declare array so that each comma separated value will be in a seperate dimension of the array ?
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, ",");
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