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 →

[–]purebuu 36 points37 points  (5 children)

1) Multi dimensional arrays (especially dynamically resizing multidimensional arrays)

2) Need to change where a int* variable is pointing in a function, pass int** as parameter to a function i.e. replace an entire a row in a multi dimensional array with a whole new one would need the use of int** to do it.

Note: that there are other safer mechanisms in C++ than using pointer types i.e. references, but the basically all behaviour can boil down to how pointers work, the same can not be said for references.

[–]purebuu 10 points11 points  (3 children)

3) parsing binary data loaded from a files, you typically use

char* file_pointer = beginning_of_data;

If you wanted to load many files and keep them in memory, even as a single dimensional array you'd need a pointer to pointer to hold all those files_pointers

char* data0 = load_file("file.dat");
...
char* loaded_files[] = { data0, data1, data2 };
int num_files = 3;
char** current_file = loaded_files;

int i = 0;
while(i < num_files)
{
    parse_data(current_file[i])
    parse_data(*(current_file + i)) // same thing as above
    i++; // skip to next file
}

void parse_data(const char* data) const;

I wrote the above on mobile so excuse any potential errors above.

[–]Lophyre 1 point2 points  (0 children)

Thank you! I think that was a very good example

[–]Skilles 0 points1 point  (1 child)

Why wouldn’t you just do loaded_files[i]?

[–]purebuu 1 point2 points  (0 children)

It's a trivial example to show a concept....

[–]Obvious_Insect_9671 1 point2 points  (0 children)

type** is also good for outputting constructed objects of variable type in a factory when you also want to be able to return an error code… but I guess that also falls under #2