Pointer Simple Example Program with Reference operator (&) and Dereference operator (*)

Pointer Definition

Pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

Accessing The Address:

& - Locate the Variable Address 

&variable 

ponter_variable = &variable; 

Declaring Pointer Variables

  • * and varibale name 
  • pointer variable points to a variable of type data_type 

Syntax:

data_type *pt_name; 

Example:

int *p; 

float *x; 

Initialization Of Pointer Variable:

pointer_vaibale = &variable; 

Reference operator ("&")

The reference operator noted by ampersand ("&"), is also a unary operator in c languages that uses for assign address of the variables. It returns the pointer address of the variable. This is called "referencing" operater. 

Dereference operator ("*")

The dereference operator or indirection operator, noted by asterisk ("*"), is also a unary operator in c languages that uses for pointer variables. It operates on a pointer variable, and returns l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer. 

1).Normal Variable

int var; 

2). Pointer Variable Declaration for Integer Data Type 

int *pt;

3). now var == 0

var = 0;

4). & takes the address of var , Here now pt == &var, so *pt == var

pt = &var; 

5). Assign Values using dereference operator

*pt = 1;  

its equivalent to var = 1, since *pt == var, now *pt == 1 and *pt == var, so var == 1

Pointer Simple Example Program for understand Reference operator (&) and Dereference operator (*)

/*##Pointer Example Program with Reference operator (&) and Dereference operator (*)*/
/*##Simple Pointer Programs,Reference operator,Dereference operator*/


#include <stdio.h>


int main(){
    
    //Pointer Variable Declaration for Integer Data Type 
   int* pt;
   int var;
   
   var=1;
   
   printf("Address of var             :%d\n",&var);
   printf("Value   of var             :%d\n\n",var);
   
   //& takes the address of var , Here now pt == &var, so *pt == var
   pt=&var;
   
   printf("Address of Pointer pt     :%d\n",pt);
   printf("Content of Pointer pt     :%d\n\n",*pt);
   
   var=2;
   
   printf("Address of Pointer pt     :%d\n",pt);
   printf("Content of Pointer pt     :%d\n\n",*pt);
   
   //Assign Values using dereference operator
   *pt=3;
   
   printf("Address of var            :%d\n",&var);
   printf("Value   of var            :%d\n\n",var);
   
   return 0;
   
}

Sample Output:

Address of var         :1565276140
Value   of var         :1

Address of Pointer pt :1565276140
Content of Pointer pt :1

Address of Pointer pt :1565276140
Content of Pointer pt :2

Address of var        :1565276140
Value   of var        :3