c - How to output a string, the result is (null)? -
this question has answer here:
how output pointer string, print result "(null)" outputting char?
printf("%s\n", *str ? str : "(null)");
if first character pointed str '\0', above print (null). otherwise, print string pointed str.
to handle case:
char *str = null;
...use:
printf("%s\n", (str && *str)?str:"(null)");
Comments
Post a Comment