Lets say we have an array of char pointers
char* array[] = { "abc", "def" };
Now what should be put in the end ?
char* array[] = { "abc", "def", '\0' };
or
char* array[] = { "abc", "def", "\0" };
Though, both works. We only have to put the condition to check the end accordingly
like
array[ index ] != '\0';
or
array[ index ] != "\0";
My question is which one is the better way? Which is used by most programmers?
Edit
Most answers say that NULL is better than ‘\0’ and “\0”.
But I always thought that
NULL is same as ‘\0’ which is same as 0x0 or 0
Is it wrong?
I would end it with
NULL. Why? Because you can’t do either of these:The first one is comparing a
char *to achar, which is not what you want. You would have to do this:The second one doesn’t even work. You’re comparing a
char *to achar *, yes, but this comparison is meaningless. It passes if the two pointers point to the same piece of memory. You can’t use==to compare two strings, you have to use thestrcmp()function, because C has no built-in support for strings outside of a few (and I mean few) syntactic niceties. Whereas the following:Works just fine and conveys your point.