How can I loop through this double pointer without knowing it's size.
char *arr[] = {"ant", "bat", "cat", "dog", "egg", "fly"};
char **ptr = arr; // Double pointer
I tried this but I get an error
while (*ptr){
printf("%s\n",*ptr);
ptr+=1;
}
I wan't something similar to this but with double pointers.
char *word = *ptr;
for (int i = 0; *(word + i) != '\0'; i++)
{
printf("%c", *(word + i));
}
The reason you can determine the end of a string with \0 is because when you assign one to a pointer a null byte is automatically placed at the end, note that the definition of string is a null terminated array of characters. You can mimic this behavior in an array of strings by adding a NULL element at the end:
char *arr[] = {"ant", "bat", "cat", "dog", "egg", "fly", NULL};
char **ptr = arr; // Double pointer
while (*ptr) {
printf("%s\n", *ptr);
ptr += 1;
}
This is what is called a sentinel.