What is the appropriate way to iterate over an array of pointers? How do you determine when you've reached the last pointer?
What is the appropriate way to iterate over an array of pointers? How do you determine when you've reached the last pointer?
The same way you would iterate over any array, e.g., with a for loop.What is the appropriate way to iterate over an array of pointers?
You need to know the size of the array. Alternatively, you leave a null pointer at the end, imitating the null character trick of strings, but at the cost of having to have such a null pointer and risking accessing the array out of bounds if it is somehow not there.How do you determine when you've reached the last pointer?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for the response.
What is the standard way of doing this in C? Suppose I have a function whose parameter list contains an array of pointers, how do I specify how many items are in that array? Does it make sense to also include an additional parameter that allows the caller to explicitly define the number of pointers? Or is there a better way of doing this?
Yes, it does. It may even be better to define a struct that has the array as one member and the array size as the other member.Suppose I have a function whose parameter list contains an array of pointers, how do I specify how many items are in that array? Does it make sense to also include an additional parameter that allows the caller to explicitly define the number of pointers?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You could pass two pointers, one to the beginning and one to the end, and loop through it with pointer arithmetic.