If I want to return an empty char*, I can do this
char* Fun1(void) {
return "";
}
Now imagine the same problem with char**, I want to return an empty array of char*.
Is there a shorter way to write this without using a temporary variable ?
char** Fun2(void) {
char* temp[1] = {""};
return temp;
// return {""}; // syntax error !
}
The goal is to hide the fact that the string can be a NULL pointer.
Don’t use a temporary variable, as it will be out of scope on return. If you have multiple functions that return the same empty array, they can return pointers to the same ’empty array’.
Also, this makes it easy to detect if the function specifically returned emptiness, by comparing pointers:-