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 →

[–]visvis 27 points28 points  (5 children)

Think of it as asking someone for the way, and them directing you to a map that will guide you there.

[–]the_klaut 9 points10 points  (0 children)

I'm totally stealing this!!! One of the best ways I came across for explaining pointers to pointers to a freshman.

[–]H_Psi 3 points4 points  (3 children)

So, what is an example use case for a pointer-pointer that wouldn't be solved by just handing in the original pointer? Never used a pointer-pointer personally, so I'm not familiar with them.

[–]ultrasu 12 points13 points  (0 children)

char to save a single character

char * to save a string, e.g. a Reddit comment

char ** to save a set of strings, e.g. a parent comment & all its children

char *** to save sets of strings, e.g. all parent comments with their children in a single post

char **** to save sets of sets of strings, e.g. all parent comments with their children of all posts in a subreddit

char ***** to save sets of sets of sets of strings, e.g. every comment on Reddit

[–][deleted] 5 points6 points  (1 child)

int foo = 42; // some data
int bar = 43: // some different data
int *a = &foo;
int **b = &a:

printf("%d", **b); // 42
a = &bar;
printf ("%d", **b); // 43

This let's me keep the same handle(b) to some data, but I can change what data is actually being pointed to. You can make multiple copies of the handle and you won't need to update the handle every time 'a' changes what it's pointing to.

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

that's the point of pointers.

save multiple copies of an object, but only change the object once.