Hello Everyone,
I am new in Renesas development and using e2studio ide. I am trying some c related logic in e2studio. I want to extract substring between two string or between two character in main string.
I have included following header files
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stddef.h>#include <stdint.h>
I am trying below function to get desired string from main string. ,
char * extract_between(const char *str, const char *p1, const char *p2){ char* i1 = strstr(str, p1);
if(i1 != NULL) { const size_t pl1 = strlen(p1); char* i2 = strstr(i1 + pl1, p2); if(p2 != NULL) { /* Found both markers, extract text. */ const size_t mlen = i2 - (i1 + pl1); char *ret = malloc(mlen + 1); if(ret != NULL) { memcpy(ret, i1 + pl1, mlen); ret[mlen] = '\0'; return ret; } } } return '\0';}
I have tested this function in other Eclips IDE and online c compiller but not getting proper value in e2studio even though I just tried below
char *v = "Sudeep Pawar"; char *subString ;strncpy(subString, v, 4);
but in substring data not copied. So I just want to know is any setting c related or string related missing. Please suggest.
Regards,
Sudeep
> but still in debug I am not seeing strncpy is working variable v is not copying in substring variable.strncpy(subString, v, 4) works as strncpy(subString, "Sudeep Pawar", 4) and results subString…
Use char subString[30] not char *subString[30]
Can I get how I can use string functions like strcat(), strncpy(), strstr() etc in renesas e2studio?
As I mentioned in my previous message, I just validating below code
char *v = "Sudeep Pawar"; char *subString="" ; char *nwstr=""; strncpy(subString, v, 4); strcat((char *)nwstr, (const char *)v); R_SCI_UART_Write(&g_uart0_ctrl, (char *)nwstr, strlen(nwstr)); R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_MILLISECONDS);
but using strcat no value concat in nwstr or no value copied in substring using strncpy function. Please suggest.
> I have also tries subString[ 30 ]; but string functions not working properly.String variable should end with NULL character (== 0), but stack memory is dirty (non-zero values are there).You need to clean up the buffer with memset( ) before use or explicitly put 0 at the end of string position.You should be aware of memory handling in C programming.
Hello Okra,
you are right String variable should end with NULL character and I have did it in my main program and always I take care of it. To your reference and for example I have mentioned here edited test code code as you suggested, Please refer I have used memset function as well as NULL attached at the end of string but still in debug I am not seeing strncpy is working variable v is not copying in substring variable.
char *v = "Sudeep Pawar"; char *subString[30] ; // char *nwstr[30]; memset(subString, '\0', sizeof(subString)); // memset(subString, '0', sizeof(subString)); // memset(nwstr, '\0', sizeof(nwstr)); strncpy(subString, v, 4); subString[4]='\0'; // strcat(nwstr, v); //R_SCI_UART_Write(&g_uart0_ctrl, (char *)nwstr, strlen(nwstr)); R_SCI_UART_Write(&g_uart0_ctrl, (char *)subString, strlen(subString)); R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_MILLISECONDS);
Same Test code is working in other eclips IDE e.g. STMCudeIDE for STM32 MCU and also working in C compiler.
> but still in debug I am not seeing strncpy is working variable v is not copying in substring variable.strncpy(subString, v, 4) works as strncpy(subString, "Sudeep Pawar", 4) and results subString = "Sude".R_SCI_UART_Write(&g_uart0_ctrl, (char *)subString, strlen(subString));May not work if subString in stack because it will be lost out of function.Global or static variables should be used as communication buffers.
Ok. I will define substring as global variable and will check result.
I have define variable as global see in sc attached, But problem is same.
Yes you are right , it is by mistake. I have removed * and now it is working as expected. Thank you so much for your help. Can you explain why it is need to define this variable as global while I am using local in other IDE and it is working fine.
> Can you explain why it is need to define this variable as global while I am using local in other IDE and it is working fine.May be because of blocking interface, not proceed until finish sending out data.
Thanks once again for your great help.