Friday, January 20, 2017

Declaration and Initialization of Chain of Pointer


In this section we will learn about more pointer and advance which will need when we will work pointer with array, string etc.

We discussed before pointer, pointer variable declaration, initialization. But that was about point to a variable. Have you ask yourself that can we do point to pointer? Yes, we can do it. Pointing to a pointer variable is known as pointer of pointer or double pointer or chain of pointer.  So it is possible to create a pointer to point to another pointer.

Declaration and Initialization of Chain of Pointer

Declaration and Initialization of Chain of Pointer

In pointer declaration, we have used a single asterisk sign before pointer variable. Similarly in double pointer variable declaration we have to used double  asterisk ('**' sign) before double pointer variable. If we want to declare an integer type double pointer that can we do by following:
int **double_pointer;
So the form of declaration of double pointer is
data_type **pointer_name;
The initialization of double pointer is similar as pointer. Which pointer, we want to point by a double pointer that must be declared and initialized before the initialization of double pointer. Let var be an integer type normal variable,  *pointer_name and **double_pointer be integer type pointer variable, double pointer variable respectively then the declaration and initialization will be as below:
int var, *pointer_name, **double_pointer;
pointer_name = &var;
double_pointer = &pointer_name;
Focus on the third line. If you don't add an ampersand ('&' sign) before pointer_name, you compiler will show an error. And also remember, double pointer can't point an integer variable, it can only point to another pointer.

Accessing the Value of double pointer

In earlier, To access the value of pointer we did it by adding an asterisk before pointer variable. Similarly, we can do it for double variable by adding double asterisk . Now run the following program.
#include <stdio.h>
int main()
{
    /*VARIABLE DECLARATION*/
    int var, *pointer_name, **double_pointer;
    /*INITIALIZATION OF VARIABLE*/
    var = 30;
    /*INITIALIZATION OF POINTER VARIABLE*/
    pointer_name = &var;
    /*INITIALIZATION OF DOUBLE VARIABLE*/
    double_pointer = &pointer_name;
    /*PRINTING THE RESULT*/
    printf("Value of var: %d\n",var);
    printf("Value of pointer_name: %d\n",*pointer_name);
    printf("Value of double_pointer: %d\n",**double_pointer);
}
Since double pointer points to another pointer and that pointer also points to a variable, this process is called chain of pointers.

Question for test:
1. What is main difference between pointer and double pointer?
2. How can we access the value of double pointer?
3. Is it need to initialize pointer variable before initialization double pointer?

Next: Expressions of Pointers and Scale Factor

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
Buy
Includes:
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion