Need help in String related function issue in e2studio.

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

  • Hello Sudeep,

    What MCU and what compiler are you using?

    Kind regards,

    Sergey

    If this response, or one provided by another user, answers your question, please verify the answer. Thank you!

    Renesas Engineering Community Moderator
    https://community.renesas.com/
    https://academy.renesas.com/
    https://en-support.renesas.com/knowledgeBase/

  • Hello Sergey,

    Thanks for your considering my query. 

    I am using MCU R7FA6M1AD3CNB and Compiler is Renesas e² studio
    Version: 2022-01 (22.1.0)
    Build Id: R20220106-1410

  • > I am using MCU R7FA6M1AD3CNB and Compiler is Renesas e² studio
    You mean GCC for ARM?



    char *v = "Sudeep Pawar";
    char *subString ;
    strncpy(subString, v, 4);


    Looks no memory is allocated to subString.
    What about  this?
    char subString[ 30 ]; /* long enough size */

  • hello Okra,

    Yes it is GCC for ARM. I am giving here configuration and components that I have set,

    Project Summary

    Board:   Custom User Board (Any Device)
    Device:  R7FA6M1AD3CNB
    Toolchain:   GCC ARM Embedded
    Toolchain Version:   10.3.1.20210824
    FSP Version:   3.6.0
    Project Type:   Flat

    Selected software components

        Board Support Package Common Files   v3.6.0
        I/O Port   v3.6.0
        Arm CMSIS Version 5 - Core (M)   v5.8.0+renesas.0.fsp.3.6.0
        Board support package for R7FA6M1AD3CNB   v3.6.0
        Board support package for RA6M1   v3.6.0
        Board support package for RA6M1 - FSP Data   v3.6.0
        Custom Board Support Files   v3.6.0
        SCI UART   v3.6.0
  • I have also tries  subString[ 30 ];  but string functions not working properly.

  • 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.