Applications of Pointers in C/C++
Prerequisite : Pointers in C/C++, Memory Layout of C Programs.
- 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
// C program to demonstrate that we can change// local values of one function in another using pointers.#include <stdio.h>voidswap(int* x,int* y){inttemp = *x;*x = *y;*y = temp;}intmain(){intx = 10, y = 20;swap(&x;, &y;);printf("%d %d\n", x, y);return0;}chevron_rightfilter_noneC++
// C++ program to demonstrate that we can change// local values of one function in another using// pointers.#include <iostream>usingnamespacestd;voidswap(int* x,int* y){inttemp = *x;*x = *y;*y = temp;}intmain(){intx = 10, y = 20;swap(&x;, &y;);cout << x <<" "<< y << endl;return0;}chevron_rightfilter_none
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++. - For accessing array elements. Compiler internally uses pointers to access array elements.
C
// C program to demonstrate that compiler// internally uses pointer arithmetic to access// array elements.#include <stdio.h>intmain(){intarr[] = { 100, 200, 300, 400 };// Compiler converts below to *(arr + 2).printf("%d ", arr[2]);// So below also works.printf("%d\n", *(arr + 2));return0;}chevron_rightfilter_noneC++
// C++ program to demonstrate that compiler// internally uses pointer arithmetic to access// array elements.#include <iostream>usingnamespacestd;intmain(){intarr[] = { 100, 200, 300, 400 };// Compiler converts below to *(arr + 2).cout << arr[2] <<" ";// So below also works.cout << *(arr + 2) <<" ";return0;}chevron_rightfilter_none
Output :300 300
- To return multiple values. Example returning square and square root of numbers.
C
// C program to demonstrate that using a pointer// we can return multiple values.#include <math.h>#include <stdio.h>voidfun(intn,int* square,double* sq_root){*square = n * n;*sq_root =sqrt(n);}intmain(){intn = 100;intsq;doublesq_root;fun(n, &sq;, &sq;_root);printf("%d %f\n", sq, sq_root);return0;}chevron_rightfilter_noneC++
// C++ program to demonstrate that using a pointer// we can return multiple values.#include <bits/stdc++.h>usingnamespacestd;voidfun(intn,int* square,double* sq_root){*square = n * n;*sq_root =sqrt(n);}intmain(){intn = 100;int* sq =newint;double* sq_root =newdouble;fun(n, sq, sq_root);cout << *sq <<" "<< *sq_root;return0;}chevron_rightfilter_none
Output :10000 10
- 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
// C program to dynamically allocate an// array of given size.#include <stdio.h>int* createArr(intn){int* arr = (int*)(malloc(n *sizeof(int)));returnarr;}intmain(){int* pt = createArr(10);return0;}chevron_rightfilter_noneC++
// C++ program to dynamically allocate an// array of given size.#include <iostream>usingnamespacestd;int* createArr(intn){returnnewint[n];}intmain(){int* pt = createArr(10);return0;}chevron_rightfilter_none - 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) - 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
Recommended Posts:
- MakeFile in C++ and its applications
- stringstream in C++ and its applications
- C++ string class and its applications
- INT_MAX and INT_MIN in C/C++ and Applications
- strchr() function in C++ and its applications
- C++ String Class and its Applications | Set 2
- Reference to a pointer in C++ with examples and applications
- Features and Use of Pointers in C/C++
- Pointers vs References in C++
- What are near, far and huge pointers?
- Pointers in C/C++ with Examples
- Smart Pointers in C++
- What are Wild Pointers? How can we avoid?
- The length of a string using pointers
- Output of C programs | Set 64 (Pointers)
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.



