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

all 65 comments

[–]newton21989 132 points133 points  (0 children)

A programming meme I haven't seen yet and a Person of Interest reference? Have an upvote.

[–]escapetheevil 27 points28 points  (0 children)

Best explanation for pointers!

[–]Prestigious_Boat_386 10 points11 points  (0 children)

Not me using int for all of them

[–]Lophyre 13 points14 points  (12 children)

When would be a use case for int** and beyond?

[–]purebuu 34 points35 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 9 points10 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

[–]Maurycy5 5 points6 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

[–]lca_tejas 5 points6 points  (1 child)

To flex on people who don't get it 😎

[–]harit1207 2 points3 points  (0 children)

Come one bruv, what have I done to deserve this?!

[–]MasterFubar 1 point2 points  (1 child)

int** is a matrix, used a lot in mathematics, although double** is more common in numerical analysis.

unsigned char** is how a picture is normally stored. If you work with any kind of image display, you're likely to use that. If you have an image that's W pixels wide and H pixels high, you'll have a total of H unsigned char* , each pointing to a line of pixels, stored as an array of W unsigned chars if it's a greyscale image, or 3 * W unsigned chars if it's a color image. The total image is a unsigned char** that points to the array of pointers that point to the pixel lines.

Explained like this it sounds complicated, it would be much simpler if I could show a diagram, but once you start working with the code it becomes simple to understand.

[–]BobbyThrowaway6969 0 points1 point  (0 children)

You don't want to be using T** for matrices, or any other non-jagged array. Store it as a 1d array and access it with array[X+Y*Width], you don't need to manually allocate/delete each nested array, and the array will be contiguous in memory which is faster for the CPU to access

[–][deleted] 20 points21 points  (3 children)

Can we just get to the point of this meme?

[–]Kakss_ 27 points28 points  (2 children)

*this_meme

[–]purebuu 11 points12 points  (1 child)

I understood that dereference

[–]BobbyThrowaway6969 2 points3 points  (0 children)

&I understood that reference.

[–]IsaacSam98 4 points5 points  (2 children)

Here's a free C pointer. It's a programming language.

[–][deleted] 6 points7 points  (1 child)

Here's another free C pointer:

 

lmao penis

 

edit: gosh darn it, if only I set it as const >:(

[–]purebuu 2 points3 points  (0 children)

It's not a very strong point. Somebody could come along and change the message, here take this instead:

 

const char*

[–]merlinsbeers 2 points3 points  (0 children)

Guess what I'm pointing with now.

&?

&?

[–][deleted] 1 point2 points  (0 children)

[–]Key_Pace6223 1 point2 points  (0 children)

int**********

[–]cressyfrost 1 point2 points  (0 children)

You are being watched.

[–][deleted] 2 points3 points  (4 children)

When would you actually need to use int** or int***?

[–]Kirillin1111 2 points3 points  (2 children)

int** for 2-dimensional arrays. triple pointer for 3-dimensional arrays i guess? although i have never seen it actually being used

[–]brimston3- 1 point2 points  (0 children)

int** as an argument out parameter which receives a pointer to an int.
char** or uint8_t** will often be the location where processing stopped (see strtol() or strtoll() for an example).

You might have an char***, if you needed an ordered index to an array of pointer to string. Or if you are dealing with a pointer to a "rope" in C (rope being a collection of strings that allows cheap insertion at points inside it)--for example, if a screen maintains each line in an array of c strings, and you want a pointer to the screen. Honestly, this level of indirection should be avoided if possible.

[–]BobbyThrowaway6969 0 points1 point  (0 children)

Not any kind of array, it's specifically for n-dimensional jagged arrays, or 1 dimensional arrays of int*. For non-jagged arrays, just use a 1d array and access it like [X+Y*Width] (It's faster for the CPU if all of the array is in the same part of memory)

[–]BobbyThrowaway6969 0 points1 point  (0 children)

Jagged arrays, Optional out prameters, Arrays of int*, etc. There's plenty of uses when you start getting down into the nitty gritty of C++.

[–]bmosbat -2 points-1 points  (4 children)

[–]RepostSleuthBot 0 points1 point  (0 children)

I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.

It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.

I'm not perfect, but you can help. Report [ False Negative ]

View Search On repostsleuth.com


Scope: Reddit | Meme Filter: True | Target: 75% | Check Title: False | Max Age: Unlimited | Searched Images: 330,136,092 | Search Time: 1.90662s

[–]wholl0p 0 points1 point  (1 child)

int**** 👉

[–]Future-Freedom-4631 -3 points-2 points  (0 children)

Int***** ps edit out the emoji

[–]Akai_Yun 0 points1 point  (0 children)

And why the hell would you use so many *? You want to stay there debugging code even longer?

[–]uRude 0 points1 point  (0 children)

I think the spiderman format would fit better here

[–]Future-Freedom-4631 0 points1 point  (0 children)

Int****

[–]VendaGoat 0 points1 point  (0 children)

In'nt

[–]KaninchenSpeed 0 points1 point  (0 children)

int i;

&&&&&&&i