Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hello,

How do you sprintf multiple char* strings? Codes below will crash if I compile.

char * m_sMDN;
char * m_sClearTextSharedSecret ;
.....
pApp->m_sClearTextSharedSecret = "asdfgkhlzx1234567890qwertyuiopbn";//testing
pApp->m_sClearTextSharedSecret += SPRINTF(pApp->m_sClearTextSharedSecret, "\n%s", pApp->m_sMDN);

Thank you.

Usage of SPRINTF is wrong. Return value of SPRINTF is int32 - The length of the resulting string in buf (NOT INCLUDING NULL TERMINATION).
Have you allocated memory for pApp->m_sClearTextSharedSecret?? SPRINTF requires that output buffer is pre allocated with required size.. The crash is because pApp->m_sClearTextSharedSecret is not allocated...
pApp->m_sClearTextSharedSecret = (char*)MALLOC((STRLEN("asdfgkhlzx1234567890qwertyuiopbn")+ STRLEN(pApp->m_sMDN)+2)*sizeof(char));
retval = SPRINTF(pApp->m_sClearTextSharedSecret, "%s %s","asdfgkhlzx1234567890qwertyuiopbn", pApp->m_sMDN);
after this SPRINTF pApp->m_sClearTextSharedSecret will have the string "asdfgkhlzx1234567890qwertyuiopbn m_sMDN)>"
eg: If string pApp->m_sMDN is "msdnvalue", then pApp->m_sClearTextSharedSecret will be
"asdfgkhlzx1234567890qwertyuiopbn msdnvalue"

Usage of SPRINTF is wrong. Return value of SPRINTF is int32 - The length of the resulting string in buf (NOT INCLUDING NULL TERMINATION).
Have you allocated memory for pApp->m_sClearTextSharedSecret?? SPRINTF requires that output buffer is pre allocated with required size.. The crash is because pApp->m_sClearTextSharedSecret is not allocated...
pApp->m_sClearTextSharedSecret = (char*)MALLOC((STRLEN("asdfgkhlzx1234567890qwertyuiopbn")+ STRLEN(pApp->m_sMDN)+2)*sizeof(char));
retval = SPRINTF(pApp->m_sClearTextSharedSecret, "%s %s","asdfgkhlzx1234567890qwertyuiopbn", pApp->m_sMDN);
after this SPRINTF pApp->m_sClearTextSharedSecret will have the string "asdfgkhlzx1234567890qwertyuiopbn m_sMDN)>"
eg: If string pApp->m_sMDN is "msdnvalue", then pApp->m_sClearTextSharedSecret will be
"asdfgkhlzx1234567890qwertyuiopbn msdnvalue"

Oceans eleven wrote:Usage of SPRINTF is wrong. Return value of SPRINTF is int32 - The length of the resulting string in buf (NOT INCLUDING NULL TERMINATION).
Have you allocated memory for pApp->m_sClearTextSharedSecret?? SPRINTF requires that output buffer is pre allocated with required size.. The crash is because pApp->m_sClearTextSharedSecret is not allocated...
pApp->m_sClearTextSharedSecret = (char*)MALLOC((STRLEN("asdfgkhlzx1234567890qwertyuiopbn")+ STRLEN(pApp->m_sMDN)+2)*sizeof(char));
retval = SPRINTF(pApp->m_sClearTextSharedSecret, "%s %s","asdfgkhlzx1234567890qwertyuiopbn", pApp->m_sMDN);
after this SPRINTF pApp->m_sClearTextSharedSecret will have the string "asdfgkhlzx1234567890qwertyuiopbn m_sMDN)>"
eg: If string pApp->m_sMDN is "msdnvalue", then pApp->m_sClearTextSharedSecret will be
"asdfgkhlzx1234567890qwertyuiopbn msdnvalue"
Thanks, I'll make the correction.

Oceans eleven wrote:Usage of SPRINTF is wrong. Return value of SPRINTF is int32 - The length of the resulting string in buf (NOT INCLUDING NULL TERMINATION).
Have you allocated memory for pApp->m_sClearTextSharedSecret?? SPRINTF requires that output buffer is pre allocated with required size.. The crash is because pApp->m_sClearTextSharedSecret is not allocated...
pApp->m_sClearTextSharedSecret = (char*)MALLOC((STRLEN("asdfgkhlzx1234567890qwertyuiopbn")+ STRLEN(pApp->m_sMDN)+2)*sizeof(char));
retval = SPRINTF(pApp->m_sClearTextSharedSecret, "%s %s","asdfgkhlzx1234567890qwertyuiopbn", pApp->m_sMDN);
after this SPRINTF pApp->m_sClearTextSharedSecret will have the string "asdfgkhlzx1234567890qwertyuiopbn m_sMDN)>"
eg: If string pApp->m_sMDN is "msdnvalue", then pApp->m_sClearTextSharedSecret will be
"asdfgkhlzx1234567890qwertyuiopbn msdnvalue"
Thanks, I'll make the correction.