is there any way to count the elements in an array of pointers to strings? with elements im refering to every word or phrase in it, here is an example of an array with 3 elements (each phrase in this case).
char *s[] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!",
};
So here the array has 3 positions (that would be 3 elements), each pointing to a memory address which is the beginning of a String but it was still difficut to determine whether the array reached the end or not, so I added a fourth element '\0', so at the end it would have 2 null characters and then implemented this function.
int cantidadElementos(char *arr){
int count=0;
while(*arr!='\0'){
while(*arr!='\0'){
arr++;
}
arr++;
if(*arr!='\0'){
count++;
}
}
return count;
}
What I try to do is bound checking, so I figured out that if I use the address of each pointer in the array I could then see the address of the string, and then see if the last value is '\0' (to check if the string reached the end), but that could not be possible since the next memory address of the array could contain garbage values, so I camed up with adding another null character at the end of the array, and then checking if there is a double '\0'.
I just find this solution horrible (works, but still horrible). This could suit for a text processor, since it is just 2 bytes more of memory that I would use to determine whether the text has reached end or not.
So going back to my question, is there any way to count the elements in an array of pointers to string? I have been searching a lot and couldn't came up with a better solution.