Applications of Pointers in C/C++

Prerequisite : Pointers in C/C++, Memory Layout of C Programs.

  1. To pass arguments by reference. Passing by reference serves two purposes
    (i) To modify variable of function in other. Example to swap two variables;

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C program to demonstrate that we can change
    // local values of one function in another using pointers.
      
    #include <stdio.h>
      
    void swap(int* x, int* y)
    {
        int temp = *x;
        *x = *y;
        *y = temp;
    }
      
    int main()
    {
        int x = 10, y = 20;
        swap(&x, &y);
        printf("%d %d\n", x, y);
        return 0;
    }

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate that we can change
    // local values of one function in another using
    // pointers.
    #include <iostream>
    using namespace std;
      
    void swap(int* x, int* y)
    {
        int temp = *x;
        *x = *y;
        *y = temp;
    }
      
    int main()
    {
        int x = 10, y = 20;
        swap(&x, &y);
        cout << x << " " << y << endl;
        return 0;
    }

    chevron_right

    
    


    Output :

    20 10

    (ii) For efficiency purpose. Example passing large structure without reference would create a copy of the structure (hence wastage of space).
    Note : The above two can also be achieved through References in C++.

  2. For accessing array elements. Compiler internally uses pointers to access array elements.

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C program to demonstrate that compiler
    // internally uses pointer arithmetic to access
    // array elements.
      
    #include <stdio.h>
      
    int main()
    {
        int arr[] = { 100, 200, 300, 400 };
      
        // Compiler converts below to *(arr + 2).
        printf("%d ", arr[2]);
      
        // So below also works.
        printf("%d\n", *(arr + 2));
      
        return 0;
    }

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate that compiler
    // internally uses pointer arithmetic to access
    // array elements.
    #include <iostream>
    using namespace std;
      
    int main()
    {
        int arr[] = { 100, 200, 300, 400 };
      
        // Compiler converts below to *(arr + 2).
        cout << arr[2] << " ";
      
        // So below also works.
        cout << *(arr + 2) << " ";
      
        return 0;
    }

    chevron_right

    
    


    Output :

    300 300
  3. To return multiple values. Example returning square and square root of numbers.

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C program to demonstrate that using a pointer
    // we can return multiple values.
      
    #include <math.h>
    #include <stdio.h>
      
    void fun(int n, int* square, double* sq_root)
    {
        *square = n * n;
        *sq_root = sqrt(n);
    }
      
    int main()
    {
      
        int n = 100;
        int sq;
        double sq_root;
        fun(n, &sq, &sq_root);
      
        printf("%d %f\n", sq, sq_root);
        return 0;
    }

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate that using a pointer
    // we can return multiple values.
    #include <bits/stdc++.h>
    using namespace std;
      
    void fun(int n, int* square, double* sq_root)
    {
        *square = n * n;
        *sq_root = sqrt(n);
    }
      
    int main()
    {
      
        int n = 100;
        int* sq = new int;
        double* sq_root = new double;
        fun(n, sq, sq_root);
        cout << *sq << " " << *sq_root;
        return 0;
    }

    chevron_right

    
    


    Output :

    10000 10
  4. Dynamic memory allocation : We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C program to dynamically allocate an
    // array of given size.
      
    #include <stdio.h>
      
    int* createArr(int n)
    {
        return new int[n];
    }
      
    int main()
    {
        int* pt = createArr(10);
        return 0;
    }

    chevron_right

    
    

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to dynamically allocate an
    // array of given size.
    #include <iostream>
    using namespace std;
      
    int* createArr(int n)
    {
        int* arr = (int*)(malloc(n * sizeof(int)));
        return arr;
    }
      
    int main()
    {
        int* pt = createArr(10);
        return 0;
    }

    chevron_right

    
    

  5. To implement data structures.
    Example linked list, tree, etc. We cannot use C++ references to implement these data structures because references are fixed to a location (For example, we can not traverse a linked list using references)
  6. To do system level programming where memory addresses are useful. For example shared memory used by multiple threads. For more examples, see IPC through shared memory, Socket Programming in C/C++, etc


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : RishabhPrabhu



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.