When should a double pointer be used in C? Can anyone explain with a example?
What I know is that a double pointer is a pointer to a pointer. Why would I need a pointer to a pointer?
|
If you want to have a list of characters (a word), you can use If you want a list of words (a sentence), you can use If you want a list of sentences (a monologue), you can use If you want a list of monologues (a biography), you can use If you want a list of biographies (a bio-library), you can use If you want a list of bio-libraries (a ??lol), you can use ... ... yes, I know these might not be the best data structures |
|||
|
One reason is you want to change the value of the pointer passed to a function as the function argument, to do this you require pointer to a pointer. In simple words, Use This may not be a very good example, but will show you the basic use:
|
|||||||||
|
When you need to change value of pointer! Obvious example:
|
|||||
|
Pointers to pointers also come in handy as "handles" to memory where you want to pass around a "handle" between functions to re-locatable memory. That basically means that the function can change the memory that is being pointed to by the pointer inside the handle variable, and every function or object that is using the handle will properly point to the newly relocated (or allocated) memory. Libraries like to-do this with "opaque" data-types, that is data-types were you don't have to worry about what they're doing with the memory being pointed do, you simply pass around the "handle" between the functions of the library to perform some operations on that memory ... the library functions can be allocating and de-allocating the memory under-the-hood without you having to explicitly worry about the process of memory management or where the handle is pointing. For instance:
Hope this helps, Jason |
|||
|
char *ch - Stores a single string..
One simpler example -
|
|||||
|
I saw a very good example today, from this blog post, as I summarize below. Imagine you have a structure for nodes in a linked list, which probably is
Now you want to implement a You may write
as your But with double pointers, you can actually write
You don't need a To make things clearer, let's follow the code a little bit. During the removal:
No matter in which case, you can re-organize the pointers in a unified way with double pointers. |
||||
|
Adding to Asha's response, if you use single pointer to the example bellow (e.g. alloc1() ) you will loose the reference to the memory allocated inside the function.
|
|||
|
Strings are a great example of uses of double pointers. The string itself is a pointer, so any time you need to point to a string, you'll need a double pointer. |
|||
|
For example, you might want to make sure that when you free the memory of something you set the pointer to null afterwards.
When you call this function you'd call it with the address of a pointer
Now |
|||||||||||||||||
|
For instance if you want random access to noncontiguous data.
-- in C
You store a pointer If |
||||
|
One thing I use them for constantly is when I have an array of objects and I need to perform lookups (binary search) on them by different fields.
Then make an array of sorted pointers to the objects.
You can make as many sorted pointer arrays as you need, then use a binary search on the sorted pointer array to access the object you need by the data you have. The original array of objects can stay unsorted, but each pointer array will be sorted by their specified field. |
|||
|
Here is a SIMPLE answer!!!!
.
.
|
||||
|
I have used double pointers today while I was programming something for work, so I can answer why we had to use them (it's the first time I actually had to use double pointers). We had to deal with real time encoding of frames contained in buffers which are members of some structures. In the encoder we had to use a pointer to one of those structures. The problem was that our pointer was being changed to point to other structures from another thread. In order to use the current structure in the encoder, I had to use a double pointer, in order to point to the pointer that was being modified in another thread. It wasn't obvious at first, at least for us, that we had to take this approach. A lot of address were printed in the process :)). You SHOULD use double pointers when you work on pointers that are changed in other places of your application. You might also find double pointers to be a must when you deal with hardware that returns and address to you. |
|||
|
Hopefully the following example will clear some concepts regarding pointers and double pointers , their differences and usage in common scenarios.
|
||||
|