initialization of pointer arrays

The below example is an initialization of a pointer array:

1
2
3
4
5
6
7
8
9
10
11
char *month_name(int n)
{
    static char *name[] = {
        "Illegal month",
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };
    return (n < 1 || n > 12) ? name[0] : name[n];
}


Here we have an array of character pointers. The pointers are stored in name[i]. And they point to the characters of the i-th string, which are stored somewhere else in memory. But I am a little confused about this. Is the character pointer pointing to the first character in the character string that is stored somewhere else in memory? I assume so because a string itself is an array of characters. And if that is the case, how does the pointer know what the last character should be? Does it just search for the null terminator \0?
johnmerlino wrote:

Is the character pointer pointing to the first character in the character string that is stored somewhere else in memory?

Yes.

johnmerlino wrote:

how does the pointer program know what the last character should be? Does it just search for the null terminator \0?

Yes.
Is the character pointer pointing to the first character in the character string that is stored somewhere else in memory?
Yes.

how does the pointer know what the last character should be?
The pointer doesn't know which is the last character in the string. It points to one character.

It is the function who take care of this.

If it wasn't initialising you could decide that the character '*' is the last character and the compilator won't argue with you as long as you use your own functions not the built-in functions.

I said
if it wasn't initialising
because when initialising the compilator by itself adds the '\0' character
Topic archived. No new replies allowed.