1

I m learning C programming and I have seen some code where folks use double pointers. I did some searching on stackoverflow from below but still have a teeny tiny question (Why use double pointer? or Why use pointers to pointers?)

In the code below, my question is I have not defined 'b' as **b. Now when I try to do (*b) I`m getting a compile error. Isnt this *(*b) same as *(address of c) because 'a' has address of 'c'.. Why do I need to define 'b' as **b to really get the value of 'c'?

#include <stdio.h>

int main()
{
    int *a, *b, c;
    c = 10;
    a = &c;
    b = &a;
    printf("*a - %d\n *(*b): %d\n," , *a, *(*b));
    return 0;
}

Help/explanation is really appreciated :)

EDIT : Thanks a lot guys for the explanation. Things are now clear :)

Community
  • 1
  • 1
peedus
  • 19
  • 3
  • 1
    When you declare a pointer to an integer, you do e.g. `int *a`. Now, there's nothing like a "double pointer", but there are things like a pointer *to a pointer* to an integer. That should hopefully give you some hints to the problem. – Some programmer dude Mar 06 '16 at 03:17
  • You've told your compiler that b is a pointer to an integer, but you are trying to set it to the address of a pointer to an integer. "a" is a pointer to an integer by "&a" isn't. If you change "int *a, *b, c;" to "int *a, **b, c;" it will compile. – hft Mar 06 '16 at 03:20
  • `int *a, *b, c;` means that `b` holds a pointer to an integers. But `b = &a;` tries to make `b` hold the address of a pointer to an integer. Which is it, does `b` hold a pointer to an integer or the address of a pointer to an integer? – David Schwartz Mar 06 '16 at 03:24

3 Answers3

0

Here is a working code.

int main()
{
int *a, *b, c;
c=10;
a = &c;
b = a;
printf("*a - %d\n *(*b): %d\n,",*a, *(b));
return 0;
}

Pointers are aptly name: they "point" to locations in memory.

When you write *b, you tell the compiler that you are going to declaring a pointer of type integer so that it points to an integer. When you use b = a, you tell the compiler that you are assigning the address of c to a as well. Actually, b is a pointer variable itself which is storing the address of an integer variable c. So, this way, you can assign the address of one variable to another pointer as well.

Now, Regarding your code,

int main()
{
int *a, **b, c; // just change it from *b to **b
c = 10;
a = &c;
b = &a;
printf("*a - %d\n *(*b): %d\n,",*a, *(*b));
return 0;
}

You need to specify the compiler that the pointer b is a pointer to a pointer variable by writing 2 *.When you write **b, you are telling the compiler that you are pointing to another pointer variable. Similarly, you can also have triple pointers and Quadrupled pointers as well.
Here is a link for your reference. http://www.c4learn.com/c-programming/c-double-pointer/

Panda
  • 2,400
  • 3
  • 25
  • 35
0

You can access the value of c using *b.. change your code littlebit..

    #include <stdio.h> 
    int main() 
    {
         int *a, *b, c; 
         c = 10; 
         a = &c; 
         b = a; 
         printf("*a :%d\n *b: %d\n,",*a, *b);
          return 0;
     }


*b,*a

defines that a and b are two pointer variable..

a=&c; 

Assign the address of the variable c in pointer a.. & stand for address of operator.

b=a;

Copy the contains of a in b.. Now b also contain the address of c..

Print *a and *b.. *a and *b means contain of a and b. So *a and *b will print value of c..

You can also use **b.. **b means b is a pointer which stores address of another pointer variable..

In that case the code will be,

#include <stdio.h> 
  int main() 
  {
        int *a, *b, c; 
        c = 10; 
        a = &c; 
        b = &a; 
        printf("*a :%d\n *b: %d\n,",*a, *(*b));
        return 0;
      }

a contains address of c and b contains address of a.. So, *a will print value of c..

As b is a double pointer we need *(*b) to access the contain of b.. *(*b) will also print the value of c..

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

In your code, you have to do int **b; because:

a is a pointer-to-integer, since a = &c.

b is a pointer-to-(pointer-to-integer), since you wrote b = &a.

Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17