tags:

views:

1086

answers:

9

How do pointers to pointers work in C? When would you use them?

A: 

A pointer to pointer is, well, a pointer to pointer.

A meaningfull example of someType** is a bidimensional array: you have one array, filled with pointers to other arrays, so when you write

dpointer[5][6]

you access at the array that contains pointers to other arrays in his 5th position, get the pointer (let fpointer his name) and then access the 6th element of the array referenced to that array (so, fpointer[6]).

akappa
pointers to pointers should not to be confused with arrays of rank2, eg int x[10][10] where you write x[5][6] you access the value in the array.
Pete Kirkham
This is only an example where a void** is appropriate.A pointer to pointer is only a pointer that points to, well, a pointer.
akappa
+2  A: 

it's a pointer to the pointer's address value. (that's terrible I know)

basically, it lets you pass a pointer to the value of the address of another pointer, so you can modify where another pointer is pointing from a sub function, like:

void changeptr(int** pp)
{
  *pp=&someval;
}
Luke Schafer
I m confused!!!
sorry, I know it was pretty bad. Try reading, erm, this: http://www.codeproject.com/KB/cpp/PtrToPtr.aspx
Luke Schafer
+4  A: 

You may want to read this : Pointers to Pointers

Hope this helps to clarify some basic doubts.

aJ
A: 

A pointer to a pointer is also called a handle. One usage for it is often when an object can be moved in memory or removed. One is often responsible to lock and unlock the usage of the object so it will not be moved when accessing it.

It's often used in memory restricted environment, ie the Palm OS.

computer.howstuffworks.com Link>>

www.flippinbits.com Link>>

epatel
+9  A: 

How do pointers to pointers work in C?

First a pointer is a variable, like any other variable, but that holds the address of a variable.

A pointer to a pointer is a variable, like any other variable, but that holds the address of a variable. That variable just happens to be a pointer.

When would you use them?

You can use them when you need to return a pointer to some memory on the heap, but not using the return value.

Example:

int getValueOf5(int *p)
{
  *p = 5;
  return 1;//success
}

int get1024HeapMemory(int **p)
{
  *p = malloc(1024);
  if(*p == 0)
    return -1;//error
  else 
    return 0;//success
}

And you call it like this:

int x;
getValueOf5(&x);//I want to fill the int varaible, so I pass it's address in
//At this point x holds 5

int *p;    
get1024HeapMemory(&p);//I want to fill the int* variable, so I pass it's address in
//At this point p holds a memory address where 1024 bytes of memory is allocated on the heap

There are other uses too, like the main() argument of every C program has a pointer to a pointer for argv, where each element holds an array of chars that are the command line options. You must be careful though when you use pointers of pointers to point to 2 dimensional arrays, it's better to use a pointer to a 2 dimensional array instead.

Why it's dangerous?

void test()
{
  double **a;
  int i1 = sizeof(a[0]);//i1 == 4 == sizeof(double*)

  double matrix[ROWS][COLUMNS];
  int i2 = sizeof(matrix[0]);//i2 == 240 == COLUMNS * sizeof(double)
}

Here is an example of a pointer to a 2 dimensional array done properly:

int (*myPointerTo2DimArray)[ROWS][COLUMNS]

You can't use a pointer to a 2 dimensional array though if you want to support a variable number of elements for the ROWS and COLUMNS. But when you know before hand you would use a 2 dimensional array.

Brian R. Bondy
+2  A: 

You have a variable that contains an address of something. That's a pointer.

Then you have another variable that contains the address of the first variable. That's a pointer to pointer.

Igor Oks
+54  A: 

Let's assume an 8 bit computer with 8 bit addresses (and thus only 256 bytes of memory). This is part of that memory (the numbers at the top are the addresses):

 45   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|    | 58 |    |    | 63 |    | 55 |    |    | h  | e  | l  | l  | o  | \0 |    |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

What you can see here, is that at address 63 the string "hello" starts. So in this case, if this is the only occurrence of "hello" in memory then,

const char *c = "hello";

... defines c to be a pointer to the (read-only) string "hello", and thus contains the value 63. c must itself be stored somewhere: in the example above at location 58. Of course we can not only point to characters, but also to other pointers. E.g.:

const char **cp = &c;

Now cp points to c, that is, it contains the address of c (which is 58). We can go even further. Consider:

const char ***cpp = &cp;

Now cpp stores the address of cp. So it has value 55 (based on the example above), and you guessed it: it is itself stored at address 60.


As to why one uses pointers to pointers:

  • The name of an array usually yields the address of its first element. So if the array contains elements of type t, a reference to the array has type t *. Now consider an array of arrays of type t: naturally a reference to this 2D array will have type (t *)* = t **, and is hence a pointer to a pointer.
  • Even though an array of strings sounds one-dimensional, it is in fact two-dimensional, since strings are character arrays. Hence: char **.
  • A function f will need to accept an argument of type t ** if it is to alter a variable of type t *.
  • There are many other reasons that are to diverse to list here.
Stephan202
+1 If this example doesn't help the op, nothing can. :)
abababa22
yes good example..i understand what they are..but how and when to use them is more important..now..
+1 well answered.
Brian R. Bondy
sheepsimulator
char* c = "hello" should be const char* c = "hello". Also it's at most misleading to say that "an array is stored as the address of the first element". An array is stored as... an array. Often its name yields a pointer to its first element, but not always. About pointers to pointers, I would simply say that they are useful when a function has to modify a pointer passed as a parameter (then you pass a pointer to the pointer instead).
Bastien Léonard
Stephan202
@Bastien: thanks. I updated the text slightly (though one could argue that I now make improper use of the word 'reference'...)
Stephan202
Unless I'm misinterpreting this answer, it looks wrong. c is stored at 58 and points to 63, cp is stored at 55 and points to 58, and cpp is not represented in the diagram.
Thanatos
You are right... so many upvotes, and nobody noticed... I'll fix it right away!
Stephan202
... done (right?)
Stephan202
Looks good. Than minor issue was all that was stopping me from saying: Great post. The explaination itself was excellent. Changing to an up-vote. (Perhaps stackoverflow needs to review pointers?)
Thanatos
Now all the problems that made me retract my upvote are fixed. So i can give you back the vote. Great :)
Johannes Schaub - litb
Johannes Schaub - litb
A: 

When a reference to a pointer is required. For example, when you wish to modify the value (address pointed to) of a pointer variable declared in a calling function's scope inside a called function.

If you pass a single pointer in as an argument, you will be modifying local copies of the pointer, not the original pointer in the calling scope. With a pointer to a pointer, you modify the latter.

Alex Balashov
+1  A: 

When covering pointers on a programming course at university, we were given two hints as to how to begin learning about them. The first was to view Pointer Fun With Binky. The second was to think about the Haddocks' Eyes passage from Lewis Carroll's Through the Looking-Glass

“You are sad,” the Knight said in an anxious tone: “Let me sing you a song to comfort you.”

“Is it very long?” Alice asked, for she had heard a good deal of poetry that day.

“It's long,” said the Knight, “but it's very, very beautiful. Everybody that hears me sing it - either it brings the tears to their eyes, or else -”

“Or else what?” said Alice, for the Knight had made a sudden pause.

“Or else it doesn't, you know. The name of the song is called ‘Haddocks' Eyes.’”

“Oh, that's the name of the song, is it?" Alice said, trying to feel interested.

“No, you don't understand,” the Knight said, looking a little vexed. “That's what the name is called. The name really is ‘The Aged Aged Man.’”

“Then I ought to have said ‘That's what the song is called’?” Alice corrected herself.

“No, you oughtn't: that's quite another thing! The song is called ‘Ways And Means’: but that's only what it's called, you know!”

“Well, what is the song, then?” said Alice, who was by this time completely bewildered.

“I was coming to that,” the Knight said. “The song really is ‘A-sitting On A Gate’: and the tune's my own invention.”

Edd
I had to read that passage a couple of times... +1 for making me think!
Ruben Steins