10

What is necessary for storing the address of a pointer?

  int a = 2;
  int *p = &a;
  int **q = &p;

Any practical use? Real time applications.

Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • The answer you are looking for is here http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer . – Santhosh Pai Aug 19 '13 at 05:34
  • 5
    The term "double pointer" is misleading please change it to pointer to a pointer :) – 0decimal0 Aug 19 '13 at 05:41
  • 1
    Now why it is put on hold? It is not really bad question. – Pranit Kothari Aug 19 '13 at 05:48
  • 1
    @Gops AB: +1 not worth to put on hold. – Pranit Kothari Aug 19 '13 at 05:49
  • Open link for ["Double pointer and pointer to pointer"](http://codepad.org/ZlMenjn2) , now you can always **delete** your [unanswered question](http://stackoverflow.com/questions/18308775/double-pointer-and-pointer-to-pointer) and get back your repo. which is my favorite feature on SO :) – Grijesh Chauhan Aug 19 '13 at 17:19

5 Answers5

23

In C you can either pass "value" or "address" of an object. If you pass value in function call, then changes made in the function don't reflect at calling place. And to reflect changes, you need to pass address of the object, for example:

void insert(node* head){
   head->data = 10;    // address(or head) is still same 
}

By object I means any type int, char or struct e.g. node

In the above example you change value at addressed by head pointer and change in data will be reflected.

But suppose if you want to change head itself in your list (e.g. insert new node as first node).

void insert(node* head){
   head = malloc(sizeof(head));  // head changed 
   head->data = 10;
}

Then value doesn't reflect at calling place because this head in function is not the same as head at calling place.

You have two choice, either return head or use pointer to pointer (but remember only one value can be return).

Use pointer to pointer:

void insert(node** head){
   (*head) = malloc(sizeof **head);   
   (*head)->data = 10;
}

Now changes will reflect!

The point is, if address is your value (need to updated address), then you need to use pointer of address or I should say pointer to pointer to reflect changes at the calling place.

As your question is what is need of pointer to pointers, one more place where pointer to pointer used in array of string, and dynamic 2-D arrays, and for same use you may need pointer to pointer to pointer for example dynamic matrix of String or/ 3D char array.

Read also this:Pointers to Pointers I just found, to tell you for an example.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    Cauhan: +1. Great answer. Practically in link list only I have used the same. – Pranit Kothari Aug 19 '13 at 05:43
  • 1
    That precisely answers what was being asked , its only one aspect of its use nevertheless +1 :) – 0decimal0 Aug 19 '13 at 05:44
  • @pranitkothari yes! I used both, but I use return header to avoid complex pointer expressions but double pointer makes code a bit small. – Grijesh Chauhan Aug 19 '13 at 05:46
  • 1
    Supposed to be ( * head) and not (head * ) – Asaf Aug 19 '13 at 16:24
  • @Mistu4u thanks very much Mistu, I notice where you made changes, Should One read [English Language Learners beta](http://ell.stackexchange.com/questions?sort=newest) regularly to improve english? – Grijesh Chauhan Sep 11 '13 at 08:54
  • @GrijeshChauhan, Meh, it does not work like that. ELL can help you finding your way out of problems you face. But you should learn and practice English yourself because _Practice makes you perfect_ and _Self-learning is the best way to learn_. Just like Datastructure :) – Mistu4u Sep 11 '13 at 13:15
  • @GrijeshChauhan Note: the last example should be `(*head) = malloc(sizeof **head);` – wildplasser Sep 11 '13 at 18:54
  • I dislike the way this seems to claim two different modes of passing. For C, there's *only* pass by value - it's just that you can choose to pass a pointer to a pointer. That "pointer to a pointer" is still passed by value. The mechanism doesn't change - just how you choose to use it. – Jon Skeet Jan 24 '14 at 08:27
  • @JonSkeet Yes I understand. Every this is pass by values only..actually I read somewhere in your answer only. – Grijesh Chauhan Jan 24 '14 at 08:29
  • Right - so why do you say that 'C uses "pass by value" and "pass by address"'? That certainly doesn't sound like 'C only uses "pass by value"' to me. – Jon Skeet Jan 24 '14 at 08:30
  • @JonSkeet I wrote this answer before I could lean that every this passes by value(from your Java answer). But actually this is not only my fault many books write this, In-fact I read in a Java book that says Java uses pass by value for simple datatype else pass by reference. I will correct this answer. – Grijesh Chauhan Jan 24 '14 at 08:34
  • 1
    @GrijeshChauhan: Yes, there are *lots* of bad Java books around. Likewise C#, where it's even more confusing because C# *does* have pass by reference, it just doesn't use it by default. Thanks for correcting the answer. – Jon Skeet Jan 24 '14 at 08:35
  • @JonSkeet Is there really any language that uses 'pass-by-reference' (or pass by address). I think I should use the terms as `In C array should 'passed by address'`. (but actually In function I am passing address value). – Grijesh Chauhan Jan 24 '14 at 09:00
  • 1
    @GrijeshChauhan: Yes, there are - look at C# for example. See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Jan 24 '14 at 09:26
  • @JonSkeet I was reading the article, I think C++ also uses "pass by reference" in addition of "pass by value". And of-course "pass by address" is still not there in C++. Am I Correct? – Grijesh Chauhan Feb 04 '14 at 06:56
  • 1
    @GrijeshChauhan: Yes, I believe C++ has pass by reference, but I wouldn't like to claim too much about it, as I haven't used C++ for a long time, and don't know the technical details. – Jon Skeet Feb 04 '14 at 06:59
12

A ** is just a pointer to a pointer. So where an *p contains the address of an p, p** contains the address of an p* that contains the address of an p object.

** is used when you want to preserve the Memory-Allocation or Assignment even outside of a function call.

Also check this article.

EXAMPLE:-

void allocate(int** p)
{
  *p = (int*)malloc(sizeof(int));
}

int main()
{
  int* p = NULL;
  allocate(&p);
  *p = 1;
  free(p);
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
4

Does

int main (int argc, char** argv) { ... }

Look familiar?

doron
  • 27,972
  • 12
  • 65
  • 103
3

If you want to allocate Nth order pointer in other function, you should use (N+1)th order pointer. For example:

void allocate_matrix(int ***ptr, int x, int y);

Lets us allocate a matrix:

int **matrix:
allocate_matrix(&matrix, 5, 5);
Useless
  • 64,155
  • 6
  • 88
  • 132
Polymorphism
  • 249
  • 1
  • 9
1

One use for this is to change the value of a pointer from within a function. For example:

#include <stdio.h>

void swapStrings(const char **strA, const char **strB)
{
    const char *tmp = *strB;
    *strB = *strA;
    *strA = tmp;
}

int main()
{
    const char *a;
    const char *b;
    a = "hello";
    b = "world";
    swapStrings(&a,&b);
    // now b points to "hello" and a points to "world"
    printf("%s\n", a);
    printf("%s\n", b);
}

Outputs:

world
hello
Wayne Uroda
  • 5,025
  • 4
  • 30
  • 35