How do pointers to pointers work in C? When would you use them?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
When a reference to a pointer is required. For example, when you wish to modify the value (address pointed to) of a pointer variable declared in a calling function's scope inside a called function.
If you pass a single pointer in as an argument, you will be modifying local copies of the pointer, not the original pointer in the calling scope. With a pointer to a pointer, you modify the latter.
You have a variable that contains an address of something. That's a pointer.
Then you have another variable that contains the address of the first variable. That's a pointer to pointer.
I like this "real world" code example of pointer to pointer usage, in Git 2.0, commit 7b1004b:
Chris points out in the comments to the 2016 video "Linus Torvalds's Double Pointer Problem " by Philip Buuck.
kumar points out in the comments the blog post "Linus on Understanding Pointers", where Grisha Trubetskoy explains:
There so many of the useful explanations, but I didnt found just a short description, so..
Basically pointer is address of the variable. Short summary code:
Also useful info can be found in topic What means reference and dereference
And I am not so sure, when can be pointers useful, but in common it is necessary to use them when you are doing some manual/dynamic memory allocation- malloc, calloc, etc.
So I hope it will also helps for clarify the problematic :)
it's a pointer to the pointer's address value. (that's terrible I know)
basically, it lets you pass a pointer to the value of the address of another pointer, so you can modify where another pointer is pointing from a sub function, like:
I've created a 5 minute video that explains how pointers work:
https://www.youtube.com/watch?v=3X-ray3tDjQ