Pointer

Pointer in c

Pointers are used to access the variables directly by using their address Locations. The value has been assigned to the variable gets stored in that particular memory location.

Pointer operators

Two types of pointer operators:

  1. address operator[&]
  2. indirection/derefrencing operator[*]
1.address operator[&]

& is used to determine the address of the variable.

Example:
int a=1;
            int *aptr;
            aptr= &a;   //aptr gets address of a   
2.indirection/derefrencing operator[*]

* can be used to assign a value to a location in memory.

Example:
*aptr=2;    //changes a to 2 

Declaration of pointer

It contain data type and pointer name with astric sign.

Syntax:
data type *pointer_name;

here, * is pointer variable.

Example :

int *ptr;

This declares that the pointer variable points to an integer data type.

Initialization of pointer

Initialization of pointer variable is done by assigning to it the address of a variable

Syntax:
pointer_name= &variable;
Example:
 int a=2081;
           int *ptr;   //Declaration of pointer
           ptr = &a;  //Initialization of pointer 

Advantages of pointer

  1. Dynamic memory Allocation
  2. Pointer provide direct access to memory.
  3. Reduce the execution time of the program.
  4. It provide an alternate way to access array elements.
  5. Using pointers, arrays and structures can be handled in more efficient way.
  6. Searchin of large size data is very efficient.

Disadvantages of pointrer

  1. Pointers are slower than normal variables.
  2. Poiner lead to memory leaks.
  3. Pointer are a litle complex to understand.
  4. If pointers are updated with incorrect values, it might lead to memory corruption.

Methods of parameter passing

Sample programs:

1.C program to addition of two number using pointer
         
         #include<stdio.h> 
         
         int main()
             {
                int first, second, *p, *q, sum;
             
                printf("Enter any two integers\n");
                scanf("%d%d", &first, &second);
             
                p = &first;
                q = &second;
             
                sum = *p + *q;
             
                printf("Sum of the two numbers = %d\n", sum);
             
                return 0;
             }
     

Output

         Enter any two integers
         2
         3
         Sum of the two numbers = 5
     
2.C Program to reverse a number using pointer
        
        #include<stdio.h> 
         
        int  main( )
        {
             int  num, rem, rev=0 ;
             int  *pn, *pr ;
             printf(" Enter the number \n") ;
             scanf("%f ",& num) ;
             pn = & num ;
             pr = & rev ;
             do
              {
                rem = ( *pn ) % 10 ;
                *pr = ( *pr * 10 ) + rem ;
                *pn = ( *pn ) / 10 ;
              }
              while(  *pn > 0) ;
                 printf("\n Reverse of Number is : %d ",*pr) ;
                 return ( 0 );
        }
    

Output

        Enter the number
        123 
        Reverse of Number is : 321
    
3.C program to find the largest number using pointer
        
        #include<stdio.h> 
        #include<conio.h> 
         
        void main()
        {
           int x,y, *large, *xptr,*yptr;
           clrscr();
           printf("Enter the value of x and y \n");
           scanf("%d%d",&x,&y);
           xptr=&x;
           yptr=&y;
           if(*xptr>*yptr)
           {
            large=xptr;
           } 
           else
           {
            large=yptr;
           } 
           printf("The largest number is : %d",*large);
           getch();
        }  
    

Output

        Enter the value of x and y 
        34
        45
        The largest number is : 45
    
Examples For Practice
  1. Find reverse of the string using pointer.
  2. Write a program to print the elements of a 1D integer array in reverse order along with memory address of each element using pointer.
    (use decrement operator on pointer to print the address)
    e.g for array containing 10,20,30…output will be
    30 at 65528……..
    20 at 65526
    10 at 65524
  3. 5) Write a program to print characters in string along with memory addresses of each character
    e.g for sting : “hello” output will be
    h at 65524
    e at 65525……..