Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pointer Arithmetic

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 536
    Comment on it

    A pointer is a variable which is used to store the address as a value of another variable. Pointers are helpful with program's efficiency and allow us to handle unlimited amounts of data.

     

    The following operations are performed with pointers:

     

    1. Addition of a number to a pointer variable:

    Suppose p is a pointer variable pointing to an element of integer type, then the statement p++; increments the value of p by a factor of 2, so that it points to the next location that holds another value of integer type. This increment factor will be 1 for character, 4 for long integer and float, and 8 for long float. The statement p+=I; where I is either a positive integer constant or an integer variable having positive value, increments p such that it points to the ith location beyond the location to which it is currently pointing.

     

    2. Subtraction of a number from a pointer variable:

    Suppose p is a pointer variable pointing to an element of integer type, then the statement p--; decrements the value of p by a factor of 2, so that it now points to the location preceding the current location. The statement p-=I; where I is either a positive integer constant or an integer variable having positive value, decrements p such that it points to the ith location before the location to which it is currently pointing.

     

    3. Subtraction of one pointer variable from another:

    One pointer variable can be subtracted from another provided both point to the same data type. The difference of the 2 indicates the number of bytes separating the corresponding elements.

     

    Example:

    *(px+3)-(px+2)
    =*(px+1)
    = 8-6 = 2 bytes

     

    Program to demonstrate the passing of an entire array using pointer notation.

     

    #include<stdio.h>
    
    void main()
    
        {
    
           int arr[10]={12,15,20,17,25,50,11,8,10,13};
    
           int i;
    
           int maximum(int *temp_arr, int size);          /*function prototype*/
    
           printf(\n largest of the array element is %d\n,maximum(arr,10));
    
         }
    
    int maximum(int *temp_arr, int size)
    
        {
    
           int temp_max, i;
    
           for(temp_max = *temp_arr, i=1; i<size, i++)
    
           {
    
             if *(temp_arr + i) > temp_max)
    
             temp-max = *(temp_arr + i);
    
           }
    
           return temp_max;
    
        }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: