I’ve been C# developer.
But Now I have to write C code.
I used to use String.IsNullOrEmpty() for preventing Errors and assign a new string.
I hope to write C code to act like String.IsNullOrEmpty. and assign a new string
so I wrote some sample code.
static char *pCtvUrl;
void set_app_url(const char* appUrl){
if(!appUrl || !*appUrl)
return;
pCtvUrl = malloc(sizeof(appUrl));
strcpy(pCtvUrl,appUrl);
}
I want to check if this is correct or not.
If you know better way to solve, please give me some advice.
thanks
Your check for a NULL or empty string is correct
Change:
to:
You also need to
free()memory ofpCtvUrlbefore reassigning it:Before
pCtvUrl = malloc(...);callfree(pCtvUrl);.Instead of using a global variable consider changing
set_app_url()to return a copy of the string:The caller would be responsible for freeing the returned string.