Pointers in C Programming

In    programming, variable is used to hole the value & this variable stored in a memory.  Each variable has a address to identify where these variables actually stored in a memory.   

 In a C programming language, pointer is a variable which can hold the address of another variable.  

Accessing Address of Variable

In a system every memory location has its address & we can access the address using ampersand (&) operator.

This ampersand (&) operator  denotes an address in memory. 

Ex:  
int  i = 20;
char c = ‘a’;

In the above example, there are two variable i & c.

C Language Pointer explanation
Fig:Memory variable and address explanation using pointer

Integer variable “i” stores the value 10 at memory location 1024(for example) and character variable “c” stores the value ‘a’ at memory location 1026.

#include

int main() {
   int i=20;
   int c='a';
   printf("\n value of  variable i=   %d", i  );
   printf("\n value of  variable c=   %c", c  );

}

OUTPUT

 value of  variable i=   20
 value of  variable c=   a

If we want to print the address of variable ‘i and c’ then we need ampersand (&) operator  .

#include int main() { int i=20; int c=’a’; printf(“\n Address of variable i = %d”, &i ); printf(“\n Address of variable c= %d”, &c ); }

OUTPUT

 Address of  variable i =   1641813104
 Address of  variable c=  1641813108

Note: When you run this program, address are printed might be different.

Example: Write a program to print the address of the variable.

#include 

int main () {

   int  v1;
   char v2;
   float v3;

   printf("\n Address of  variable v1=   %d", &v1  );
   printf("\n Address of  variable v2=   %d", &v2  );
   printf("\n Address of  variable v3=   %d", &v3  );

   return 0;
}

OUTPUT

 Address of  variable v1=   1174657312
 Address of  variable v2=   1174657313
 Address of  variable v3=   1174657308

Note: Each and every time when you run this program these variables stored in a different memory location.

Pointer Variable

pointer  variable is hold the address of another variable.

Declare a pointer variable

To declare a pointer variable, there must use  a *  (Asterisk) before variable name.


#include 

int main () {

   int  i= 20;		// declare & initialize variable i  
   int *ptr;		// declare a pointer variable ptr
   
   ptr = &i;		//  ptr (pointer variable) holds the address of variable i.
    
   printf("\n Address of  variable i=  %d", &i  );
   printf("\n Address of  variable i=  %d", ptr );


   return 0;
}

OUTPUT

 Address of  variable i=  511276620
 Address of  variable i=  511276620

Value at address

 Pointer pointing  to variable address
Fig: Pointer Pointing to a address

In the above example, there are two variable, first is normal variable ‘i’ & pointer variable ‘ptr’.

Integer variable “i” stores the value 10 at memory location 1024(for example) and pointer variable “ptr” stores the address of variable  ‘i’ at memory location 2322

Example

#include 

int main () {

   int  i= 10;		// declare & initialize variable i  
   int *ptr;		// declare a pointer variable ptr
   
   ptr = &i;		//  ptr (pointer variable) holds the address of variable i.
   printf("\n value of  variable i=  %d", *ptr );

   return 0;
}

OUTPUT

value of  variable i=  10

Note: Pointer variable “ptr” prints the address of variable and “*ptr” prints the value of i. *variable name (*ptr) is also called value at address.

 Pointer in C Programming
Fig: Pointer in C Programming
#include
int main() 
{
   int a=4;
   int *b;
   b = &a;
   printf("\n Address of a = %d", &a);
   printf("\n Address of a = %d", b);
   printf("\n Address of b = %d", &b);
   printf("\n Value of b = %d", b);
   printf("\n Value of a = %d", a);
   printf("\n Value of a = %d", *(&a));
   printf("\n Value of a = %d", *b);

return 0;   
}

OUTPUT

 Address of a = 2464
 Address of a = 2464
 Address of b = 2466
 Value of b =  2490
 Value of a = 4
 Value of a = 4
 Value of a = 4

Note: When you run this program, address are printed might be different.