下面是我的示例代码。它只是一个示例,类似于我在应用程序中使用的代码。
#define STR_SIZE 32
void someThirdPartyFunc(const char* someStr);
void getString(int Num, const char* myStr)
{
char tempStr[] = "MyTempString=";
int size = strlen(tempStr) + 2;
snprintf((char*)myStr, size, "%s%d", tempStr, Num);
}
int main()
{
const char * myStr = new char(STR_SIZE);
getString(1, myStr); // get the formated string by sending the number
someThirdPartyFunc(myStr); // send the string to the thirdpartyFunction
delete myStr;
return 0;
}
如果我使用此代码,我会遇到异常。我认为问题在于删除“myStr”。但是删除确实是必要的。
有没有其他方法可以格式化 getString 中的字符串并将其发送到 ThirdPartyFunc?
提前致谢。