Is it fine to use char pointer(char *retStatus) in below way? Like assigning/rewriting values whenever required without allocating memory? I tested it and it is working fine but would like to know is this a good approach to assign error messages to char * and then copy/concat to other static or allocated memory pointer.
void fn(char *status, size_t maxLen)
{
char *retStatus = NULL;
...
...
if(failure)
{
retStatus = "error1";
if((strlen(retStatus) + strlen(status)) < maxLen)
{
strcat_s(status, maxLen, retStatus);
}
}
...
...
if(failure)
{
retStatus = "error2";
if((strlen(retStatus) + strlen(status)) < maxLen)
{
strcat_s(status, maxLen, retStatus);
}
}
}
int main()
{
char status[10] = { 0 };
size_t statusMaxLen = sizeof(status) / sizeof(status[0]);
fn(status, statusMaxLen);
return 0;
}