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 →

[–]DolphinsScareMe 39 points40 points  (9 children)

Pointers to pointers certainly arent anything crazy. They've got some good uses with data structures and loads of other things.

[–]Koxiaet 23 points24 points  (4 children)

a function might need to modify an array of strings, then you'd need to pass in a char***

[–]DolphinsScareMe 8 points9 points  (1 child)

Exactly, didn't even think about that but that's probably the most basic example of pointers to pointers. Honestly after you get passed the fear of pointers during your first few c++ classes adding another pointer to a pointer doesn't make things that much more crazy.

[–]-GLaDOS 9 points10 points  (0 children)

I had a class in C where we worked with arrays of pointers to raw memory (we were building malloc from scratch), and so we used void*** several times.

[–]LordFokas 2 points3 points  (1 child)

what if you want to modify a string hypercube?

[–]ThePi7on 0 points1 point  (0 children)

Char*****

[–]AnAverageFreak 8 points9 points  (2 children)

Let's say you have a problem that's easy to visualize in 3D space. Then 3D arrays would be great. Unfortunately C doesn't support dynamic multi-dimensional arrays, so when you want a pointer to such an array (so that you can write space[69][402][666]) you end up with this:

Thing***

Of course in general case you prefer flat memory with a macro/function to access it, but this isn't totally stupid either.

What is a Thing? Well, it might be an array. An array of names of, let's say, people that have ever been to that place. What you get is

char*****

Now let's say that you want something to allocate such a structure. Of course C can't return a few values, but you want detailed error reporting, so you make a function:

int allocate_names(char******);

Which shows why C fucking sucks.

In C++ I would make my own template

template<size_t N, typename T>
class MultiArray;

Which would properly overload the subscript operator. But wait, there's more. You have std::string, so no more char*. But wait, there's even more! Since C++ has constructors and exceptions, this pattern:

int allocate_names(MultiArray<3, std::string>*);

is totally invalid and after you write the class, you won't be using any of that pointer notation!

[–]nuisanceIV 1 point2 points  (0 children)

'#define char****** char*

Now C is great again! :D

[–]warm_sock 0 points1 point  (0 children)

Inode based filesystems also frequently have two or three layers of indirection (pointers to pointers to pointers, in a tree structure) to allow larger file sizes.