[Solved] How to test for the end of a pointer array?


Not quite sure what output you want, but probably you want to output the strings in the array.

You can’t detect the end of an array directly.

You either need to put a sentinel value at the end of the array, for example NULL:

#include <stdio.h>

int main(void) {
    char *t[10] = { "Program", "hjl", "juyy", NULL };
                                             //^sentinel value
    int i;

    for (i = 0; t[i] != NULL; i++) {
        printf("%d: %s\n", i, t[i]);
    }

    return 0;
}

or you need tot count the number of elements, but this works only if the number elements of the initializer list of your t array is the same as the length of the array, which is not the case in your code:

#include <stdio.h>

int main(void) {
    char *t[] = { "Program", "hjl", "juyy"};
         // ^ no size here means that the array will have 
         //   the size of the initalizer list (3 here),
    int i;

    for (i = 0; i < sizeof(t)/sizeof(t[0]); i++) {
        printf("%d: %s\n", i, t[i]);
    }

    return 0;
}

sizeof(t) is the size of the t array in bytes.
sizeof(t[0]) is the size of the first element of t, which is here sizeof(char*) (size of a pointer).

Therefore sizeof(t)/sizeof(t[0]) is the number of elements of the t array (3 here).

The output of both versions will be:

0: Program
1: hjl
2: juyy

solved How to test for the end of a pointer array?