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

Wednesday, December 9, 2020

C program to declare, initialize, and demonstrate the use of pointers

C program to demonstrate the use of pointer | welcome2protec.com

Write a C program to declare, initialize and demonstrate the use of pointer. How to access value and address of a variable using pointer in C programming.

Required knowledge:

Accessing memory location of any variable using & operator

We know that address of operator gives the address of any variable. We can prefix the address of operator (&) with the variable name to get the memory location of a variable like this:

/*C program to print value and address of each variable
 *uisng address of (&) operator */.
int main()
{
 int x = 10;
 char y = 'C'
 float fl = 10.50f
 
 /*Printing the value and address of each variables*/
 printf("Value of x = %d  and address of x = %p\n" , x, &x);
 printf("Value of y = %c  and address of y = %p\n" , y, &y);
 printf("Value of fl = %.2f  and address of fl = %p\n" , fl, &fl);
 
 return 0;
}

Note: You can use format specifier %p, %x, and %u to print the address in hexadecimal format.

Output

Value of x = 10       and address of x = 0x1ff1
Value of y = C        and address of y = 0x2ff2
Value of fl = 10.50   and address of fl = 0x3ff3

Accessing value of variable using pointer

To display the value of var using pointer, the unary operator * is used which returns the value of variable var whose address is stored by the pointer (i.e. *ptr). Let's have a look at the below example.

Use of pointer | welcome2protec.com
Use of pointer | welcome2protec.com
/*C program to to print value and address of var using pointer*/
#include <stdio.h>  
int main(){ 
 
  int  var = 10;  /* Declaration of normal variable */ 
  int  *ptr;      /* Declaration of pointer variable */ 
  
 /* storing address of var in pointer variable 
  * (or can say initialization of pointer)*/ 
  ptr = &var;  
 
 /*Printing value and address of var using & operator*/
  printf("Value of var = %d  and Address of var = %x\n", var, &var);
  
  /*Printing value and address of var using pointer*/
  printf("Value of var = %d  and Address of var = %x\n", *ptr, ptr);
 
  /* printing the address of pointer variable i.e. ptr */ 
  printf("Address pointer ptr variable: %x\n", &ptr );
  
  /* Let's change the value of var using pointer */ 
  *ptr = 20;
  printf("Value of var has now been changed to %d\n", *ptr );
 
  return 0; 
} 

Output

Value of var = 10  and  Address of var = 0xbffd8b3c4c
Value of var = 10  and  Address of var = 0xbffd8b3c4c
Address of ptr = 0xbffd8b3c50 
Value of var has now been changed to 20








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