Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, December 7, 2020

Pointer to pointer in C

Pointer to pointer | welcome2protec.com

As we already know that, pointer is used to store address of another variable. However, we can also declare a pointer variable to store address of another pointer, yes it is possible in C. When a pointer holds the address of another pointer variable this is known as double pointer or pointer to pointer. Let's see the below diagram to understand it better way:


Pointer to pointer Or Double pointer | welcome2protec.com
Pointer to pointer Or Double pointer | welcome2protec.com

Here is the syntax for declaring a double pointer (or pointer to pointer):

/* pointer to a pointer which is pointing to an integer var */
int **p
Let's understand with a simple example given at below:

Example of double pointer:

Double pointer or pointer to pointer is a form of multiple indirection, or chain of pointers as you can see in the above diagram. Let's have a look at the below example based on above diagram.

#include <stdio.h> 
int main(){ 
 
  int  var = 10; /*Actual variable declaration*/ 
  int  *p;      /*Normal pointer declaration*/ 
  int **q;	/*Double pointer declaration*/
 
  p = &var; //Assigning the address of var in pointer p

  /*Assigning the address of pointer p in double pointer q */ 
  q = &p;  
 
  /*Possible ways to find the value of var*/ 
  printf("Value of var = %d\n", var);
  printf("Value of var = %d\n", *&var);
  printf("Value of var using pointer p = %d\n", *p); 
  printf("Value of var using pointer q = %d\n", **q);
 
  /*Possible ways to find the address of var*/ 
  printf("Address of var = %x\n", &var);
  printf("Address of var uisng pointer p = %x\n", p); 
  printf("Address of var using pointer q = %x\n", *q);  

  /*Print the value of poiners p and q*/ 
  printf("Value stored at pointer p = %x\n", p); 
  printf("Value stored at pointer q = %x\n", q );

  /*Print the address of poiners p and q*/ 
  printf("Address of pointer p = %x\n", &p); 
  printf("Address of pointer q = %x\n", &q );
 
  return 0; 
}  

When the above code will compile and run following output will be produced

Value of var = 10
Value of var = 10
Value of var = 10
Value of var = 10

Address of var = 0xbffd8b3c4c
Address of var uisng pointer p = 0xbffd8b3c4c
Address of var uisng pointer q = 0xbffd8b3c4c

Value stored at pointer p = 0xbffd8b3c4c
Value stored at pointer q = 0xbffd8b3c50

Address of pointer p = 0xbffd8b3c50
Address of pointer q = 0xbffd8b3c49

--

More on this to be updated soon...

--

--



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us