I was wondering what would be the best and/or fastest way to NULL check multiple file pointers and rule out the 'bad' (NULL) ones? Can this be achieved via the switch
statement?
My 'normal/basic' approach would be:
FILE *fp1, *fp2, *fp3;
fp1 = fopen(foo, bar);
/* etc.. */
if (!fp1)
/* do something */
return 1;
if (!fp2)
return 2;
...
.. but this approach just seems too long, especially if there's too many pointers to check. Is there a trick to do this more conveniently?
In other words (or code), something like this:
if (!fp1 || !fp2 || !fp3) {
/* one of the pointers is NULL, let's *somehow* check which one it is */
} else {
/* everything OK */
}
I'm a beginner and I was thinking of using the switch
statement. To be more specific, I was thinking of (again) somehow comparing the file pointers to NULL, but I can't figure out how I'd write such code, since the expression used in switch
statement must be an integer, but on the other hand, doesn't NULL
equal 0?
I apologize, since this seems to be a trivial question, but I couldn't find anything on null checking multiple pointers like this.
Thanks in advance!