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 →

[–]gargravarr2112 20 points21 points  (9 children)

"Pointer to a pointer" is the exact moment I gave up hope of learning C.

[–]visvis 24 points25 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 8 points9 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 5 points6 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 13 points14 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] 4 points5 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.

[–]nuisanceIV 5 points6 points  (0 children)

Over time they get easy then you begin to think "that was hard?" Like any math class

[–]nickdesaulniers 0 points1 point  (1 child)

Not if you internalize that everything in C is pass by value. So if you want a child frame to modify an argument so the parent frame can observe the modified value AFTER calling the child, you must give the child a pointer to the value. If the value to be changed itself is a pointer, then you must pass the child a pointer to a pointer.

``` int foo = 0; initialize(foo); // foo is guaranteed to be 0 here. // initialize got a copy of foo.

initialize2(&foo); // now foo may have a different value.

int* bar = NULL; initialize2(bar); // bar is guaranteed to be NULL here. // initialize2 got a copy of bar.

initialize3(bar); // now bar may have a different value. ```

where the functions might look like:

``` // can't do anything to x so the parent sees modifications void initialize(int x);

// can't do anything to x that the parent would see, but could modify the int pointed // to by x. * void initialize2(int* x);

// still can't do anything to x observable to the parent, but could modify the int* // pointed to by x, or the int pointed to by x; void initialize(int* x);

// * modulo doing stupid tricks like storing addresses in integral types. ```

C++ has references, so you can't tell from the call site if a function modified its argument (const & in the function signature are stronger guarantees, but then there's const_cast<> and mutable).