Understanding double pointers

From: Wouter van Teijlingen (woutert_at_nedlinux.nl)
Date: 02/26/05


Date: Sat, 26 Feb 2005 12:51:59 +0100

Hello,

I was reading a document from the Stanford library and it's great. But
there's one thing about the document i don't understand. They write
about the ** Case. The document writes the following:

''The ** Case
What if the value of interest to be shared and changed between the
caller and callee is already a pointer, such as an int* or a struct
fraction*? Does that change the rules for setting up reference
parameters? No. In that case, there is no change in the rules.
They operate just as before. The reference parameter is still a pointer
to the value of interest, even if the value of interest is itself a
pointer. Suppose the value of interest is
int*. This means there is an int* value which the caller and callee want
to share and change. Then the reference parameter should be an int**.
For a struct fraction* value of interest, the reference parameter is
struct fraction**. A single dereference (*) operation on the reference
parameter yields the value of interest as it did in the simple cases.
Double pointer (**) parameters are common in linked list or other
pointer manipulating code were the value of interest to share and change
is itself a pointer, such as a linked list head pointer.''

So, after reading the above, i tried to understand it, but i don't
really know if i really get it. So i made the following program:

#include <iostream>
using namespace std;

void B(int **worthRef) {
    **worthRef = **worthRef +1;
}

void A(int **worthRef) {
      **worthRef = **worthRef +1;
      B(worthRef);
}

int main()
{ int worth = 55;
          int *netWorth = &worth; // value of interest
          A(&netWorth);
          cout << *netWorth;
          return 0;
}

In this case, netWorth is a pointer which points to the integer worth,
containing the value 55. This example uses double pointers, because
worthRef has to ''dereference'' twice. But... I can accomplish the same
using the following code:

#include <iostream>
using namespace std;

void B(int *worthRef) {
    *worthRef = *worthRef +1;
}

void A(int *worthRef) {
      *worthRef = *worthRef +1;
      B(worthRef);
}

int main()
{ int worth = 55;
          int *netWorth = &worth; // value of interest
          A(netWorth);
          cout << *netWorth;
          system ("PAUSE");
          return 0;
}

The difference is that in the second example i don't have to use the &
parameter. Because netWorth is already a pointer. My question really is
if i use the ''double'' pointer in a good way.

Thanks in advance,

Wouter van Teijlingen
The Netherlands



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... int create(int id, int scale, unsigned char *image); ... unsigned char* ptrImage = NULL; ... // Read through entire pointer byte by byte and put into managed array ...
    (microsoft.public.dotnet.framework.interop)
  • Re: C# - getting binary data from .lib
    ... then there is a problem with the ptrImage ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)