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?
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?
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
**
when you want to preserve (OR retain change in) the Memory-Allocation or Assignment even outside of a function call. (So, Pass such function with double pointer arg.)This may not be a very good example, but will show you the basic use:
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.
Why double pointers?
The objective is to change what studentA points to, using a function.
the following example, that i am giving will give an insight or an intuition about how double pointers work, i will go through the steps
the following is the code to which you can relate to above points(a,b,c) to understand
now to print the value i.e strings in the array, just look at point b, In case of string the value as well as address is same, so no need to dereference it again.
now to print the next string, come out of one dereference i.e (*) from *str to str and then increment , like shown below
now print the string
now to print only characters in a string, refer to point c)
now to print the next character of a string i.e "b" come out of 1 dereference operator and increment it i.e going from **str to *str and do *str++
now print the character
since the two arrays("abcdefghij","xylmnopqr") are stored in continous block of memory if same thing is done of incrementing the address, all characters of two strings will get printed
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.
The reason it occurs like this is that in
alloc1
the pointer is passed in by value. So, when it is reassigned to the result of themalloc
call inside ofalloc1
, the change does not pertain to code in a different scope.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
myMemory
is set to NULL and any attempt to reuse it will be very obviously wrong.