Firstly, we don't "add NULL character" at the end of the string. There's no such thing as "NULL character". We add zero character, which is sometimes called "NUL character". But NULL
has absolutely nothing to do with it. NULL
is normally used in pointer context and not in character or integer context. Your comparisons like *myCharPtr != NULL
or *myIntPtr != NULL
will compile (due to the way NULL
is defined in C++), but make virtually no sense. If you are looking for a zero character in an array, you can check for it as *myCharPtr != '\0'
or as *myCharPtr != 0
or as simply *myCharPtr
, but never as *myCharPtr != NULL
.
Secondly, the zero character is called zero character for a reason: it is equal to integer zero. Character type in C++ is just a plain integer type after all. The only reason we can use zero character as something special in string context is because it's meaning is reserved for that specific purpose. In general case in integer context reserving zero for that purpose is plainly impossible for obvious reasons: zero is as useful as any other integer value. Yet, if in your specific application integer zero can be used as a reserved value, feel free to use it that way. Or you can use any other integer value for that purpose. But in general case, referring to the question you ask in the title, there's no way to determine the end of an array. It is your responsibility to know where the end is (by knowing the total number of elements or by marking the end with a reserved value of your choice or in some other way). There's no way to determine the end of an array even with strings, because all you can hope for is to find the end of the string, which is not necessarily the end of the array that stores that string.
If you explicitly added a zero to the end of your integer array, your first cycle would happily stop at it. For some reason you explicitly added \0
at the end of your character array (and the second cycle stops), but you didn't add a zero at the end of your integer array (and the first cycle doesn't stop). You are wondering why your first cycle didn't stop at zero? Becuse you didn't put that zero in there. It is that simple.