The example in your edit and your original code are really two completely different things.
In C, a char[]
is used to represent a "string"; a bunch of bytes that are going to be mapped to some character set by the terminal or windowing system for human consumption.
Strings are "null terminated"; by placing a 0
in the array, functions that are operating on the array know that indicates the end of the string. (This is glossing over things a bit, as 0
is defined explicitly to terminate strings in C, and UTF-8 and US-ASCII for example explicitly define 0
as the "null character").
Your first example is dealing with int
... there's no terminating value here, unless you define one as such. This is going to entirely depend on the expected inputs to the method. A simple example would be to define -1
as your "poison pill" and check for that:
Lets say you redefined your array to be:
int v[] = {0,1,2,3,4,5,6,7,8,8,8,9,-1};
Now your while statement would look like:
while(*p_int_array != -1) {
Edit from comments:
Since you edited your Q to be entirely about strings ... A string literal is automatically null terminated:
char *foo = "hi!";
is really:
char foo[] = {'h','i','!', 0}; // or, '\0'
That said, the example you now show is wrong, and will seg-fault. It needs to be looking at what p
is pointing at, not the pointer (memory address) itself.
while (*p) {