Home Cryptocurency ExchangesApplications of Pointers in C/C++
Applications of Pointers in C/C++

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

    #include

    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 %dn”, x, y);

        return 0;

    }

    C++

    #include

    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 << "http://www.geeksforgeeks.org/" << y << endl;

        return 0;

    }

    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

    #include

    int main()

    {

        int arr[] = { 100, 200, 300, 400 };

        printf(“%d “, arr[2]);

        printf(“%dn”, *(arr + 2));

        return 0;

    }

    C++

    #include

    using namespace std;

    int main()

    {

        int arr[] = { 100, 200, 300, 400 };

        cout << arr[2] << "http://www.geeksforgeeks.org/";

        cout << *(arr + 2) << "http://www.geeksforgeeks.org/";

        return 0;

    }

    Output :
    300 300

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

    C

    #include

    #include

    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 %fn”, sq, sq_root);

        return 0;

    }

    C++

    #include

    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 << "http://www.geeksforgeeks.org/" << *sq_root;

        return 0;

    }

    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

    #include

    int* createArr(int n)

    {

        int* arr = (int*)(malloc(n * sizeof(int)));

        return arr;

    }

    int main()

    {

        int* pt = createArr(10);

        return 0;

    }

    C++

    #include

    using namespace std;

    int* createArr(int n)

    {

        return new int[n];

    }

    int main()

    {

        int* pt = createArr(10);

        return 0;

    }

  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

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.

Source

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.