I want to implement the following function to print the contents of several char
strings which are referenced through a pointer array. How can I determine how many pointers there are without passing the total as an argument of the function?
If it was an array of type int
then it would be possible to use the sizeof()
function but given that each item of my array is a pointer to a char
string and each string could be of different length I don't think I can use this approach.
void printCharArray(char *arr[]){
int length = sizeof(arr) / sizeof(char); /* this does not give correct
number of items in the pointer array */
for (int i=1;i<=length; i++) {
printf("Array item: [%s]",arr[i]);
}
}