3

I am beginner of C Programming language. I saw a code on the book:

#include<stdio.h>
int main(){
    int * * k, *a, b=100;

    a = &b;
    k = &a;
    printf("%d\n",* * k);
}

I don't know the meaning of int * *k. Is that a integer pointer or value? what will it point to? what will it contains/store? what's the use of this variable? How can I understand this expression?

msc
  • 33,420
  • 29
  • 119
  • 214
Bing Sun
  • 205
  • 1
  • 3
  • 10

5 Answers5

8
int **k

k is a pointer to pointer to int(double pointer) and holds an address of some other pointer variable.

In your example:

 int  b = 100;   /* 'b' is an int, initialized to value 100 */
 int *a = &b;  /* a is a pointer-to-int*/
 int **k = &a; /* k is a pointer-to-pointer-to-int */

See below picture for better understanding:

ptr

msc
  • 33,420
  • 29
  • 119
  • 214
2

int** k is a pointer to an int pointer.

It stores a memory address, in that memory address there another memory address in which some integer value is stored.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
1

It's called double pointer. It can be used to store address from single pointer.

You can also create triple pointer to store address from double pointer.

Example: int ***k;

algojava
  • 743
  • 4
  • 9
1

This is called pointer to a pointer.

Here, the output for **k is 100, the value of b.

  *(*k) = *(a) = *(address of b) = value of b
Pooja N
  • 11
  • 1
1
*k

means some code will get a value from address k later.

*(*k)

means some code will get a value from address (*k) later.

int **k

means k is intended to be used for address dereferencing for integer use but with a second level. Having this * character just behind a variable name at its definition, makes it a pointer. So k is a pointer to a pointer to an integer.

To get the value of cell that a pointer points to,

*k

is used just like in the definition. Then when it is a second-order pointer then

**k

is used to get its pointed value.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97