Misho Tek Misho Tek - 3 years ago 213
C Question

Return empty string in c

In my function I want to return an empty string

""
, function looks like this:

char *myFunction(nodeType *arg){
if (something){
return anotherFunction(arg);
} else {
return EMPTYSTRING;
}
}


(EMPTYSTRING should be replaced with correct expression to return
""
)


Some ways I came up with:


return ""

return '\0'





What is the right way to return an empty string?

Answer Source

It is not good idea to return "". Because, it is const char and you may try to dealloc it in where you get it from the function and this likely crashes your application.

So, I would return strdup(""), so you can free it safely.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download