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?
Hopefully the following example will clear some concepts regarding pointers and double pointers , their differences and usage in common scenarios.
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
remove_if
function, which accepts a removal criterionrm
as one of the arguments and traverses the linked list: if an entry satisfies the criterion (something likerm(entry)==true
), its node will be removed from the list. In the end,remove_if
returns the head (which may be different from the original head) of the linked list.You may write
as your
for
loop. The message is, without double pointers, you have to maintain aprev
variable to re-organize the pointers, and handle the two different cases.But with double pointers, you can actually write
You don't need a
prev
now because you can directly modify whatprev->next
pointed to.To make things clearer, let's follow the code a little bit. During the removal:
entry == *head
: it will be*head (==*curr) = *head->next
--head
now points to the pointer of the new heading node. You do this by directly changinghead
's content to a new pointer.entry != *head
: similarly,*curr
is whatprev->next
pointed to, and now points toentry->next
.No matter in which case, you can re-organize the pointers in a unified way with double pointers.
For instance if you want random access to noncontiguous data.
-- in C
You store a pointer
p
that points to an array of pointers. Each pointer points to a piece of data.If
sizeof(T)
is big it may not be possible to allocate a contiguous block (ie using malloc) ofsizeof(T) * n
bytes.Simple example that you probably have seen many times before
In the second parameter you have it: pointer to pointer to char.
Note that the pointer notation (
char* c
) and the array notation (char c[]
) are interchangeable in function arguments. So you could also writechar *argv[]
. In other wordschar *argv[]
andchar **argv
are interchangeable.What the above represents is in fact an array of character sequences (the command line arguments that are given to a program at startup).
See also this answer for more details about the above function signature.
application of double pointer as shown by Bhavuk Mathur seems to be wrong. Here following example is the valid one
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