-4

What is the difference between a pointer and a double pointer

Why would the second one ever be used?

char *a; //Points to a char
char **b; //Why doesn't this also point char
iksajotien
  • 991
  • 18
  • 24
Andrei Tumbar
  • 490
  • 6
  • 15

3 Answers3

2

A "double pointer" is just a pointer to another pointer. In your example, a pointer that points to a pointer that points to a char.

EDIT Actually this was answered here.

Community
  • 1
  • 1
Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
1

First of all, let me tell you, double pointer is not a correct nomenclature for a pointer to pointer. At times, they can mean different things (double pointer == pointer to a double) and create confusions.

A pointer points to a variable or a function. Now, a pointer itself is a variable, so there can be another pointer which points to the pointer variable. The later is called a pointer-to-pointer.

So, in case of

char *a;

a is a pointer which points to a char.

Thus,

char **b;

b is a pointer which points to a char * and hence, they are different.

You can write

b = &a;
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

b is a pointer to a pointer to a char. More common than most double pointers, since char * is typically used to reference strings, and a char ** will point to that.

Red Alert
  • 3,786
  • 2
  • 17
  • 24