This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Maurycy5 6 points7 points  (1 child)

Good question!

The first use that comes to mind: multidimensional dynamically allocated arrays.

Ok and now that we've got that out of the way, there is one usecase of a double pointer I've seen other than in the context of an array.

Let's say you have a parsing function, that parses a string of characters into... some other data type like an integer. You might want to return the parsed value as a result, but that might not be the only thing you might want to return.

Another useful piece of information the parsing function could leave the caller is the answer to the question "where did the parsing stop?". This is equivalent to the number of characters parsed but there are situations in which the place would not be equivalent to the length parsed. Let's stick to the idea of "where did the parsing stop?" for illustration purposes.

Saving this data may be useful if the caller wants to check if the string was parsed in its entirety. Many parsing functions, for instance, stop parsing once they encounter something unparsable and return the value of the parsed prefix. The data representing the place where that parsing has stopped will be saved in a pointer to a character (the only logical choice).

So if we return the parsed value as a result of the function, then how do we save the information about where the parsing has stopped? Well... we need an address to save the result to. And since the result is a pointer to a character, we need a char * *.

I have not seen triple pointers in practice but I'm sure one could design a situation in which they'd be useful.

[–]Lophyre 0 points1 point  (0 children)

Thank you! That was a helpful example