![]() |
A pointer is a variable that stores the memory address as its value. It points to the location in the computer’s memory where the actual data is stored. Pointer is one of the core features of the C++ programming language which enables various functions like dynamic memory allocation, pass-by-reference, etc. In this article, we will look at some major applications of pointers in C++. Syntax of PointersWe can simply declare a pointer of any type by using a * as a prefix as shown: dataType *pointerName = &variableName; To Know more refer to C++ Pointers Application Of Pointer in C++Pointers are one of the most powerful tools of the C++ programming language. They are used for a wide number of purposes some of which are given below: 1. Passing Arguments by ReferencePointers in C++ are commonly used to pass arguments by reference to functions. By passing the variables by reference we can modify the actual value of the variable passed to the function, rather than working with a copy of the value. Example The below example demonstrates the use of a pointer to change the local values of one function in another. C++
Output
Original value is: 10 Modified value is: 15 2. For Dynamic Memory AllocationIn C++, dynamic memory allocation is very important application of pointer that allows programs to request and manage memory during runtime. The new keyword is used to allocate the dynamic memory, and it returns a pointer to allocate memory. To deallocate memory, the delete keyword is used with pointers pointing to dynamically allocated memory. ExampleThe below example illustrates the use of pointers in dynamic memory allocation. C++
Output
Value stored in dynamically allocated memory: 42 Values stored in dynamically allocated array: 1 2 3 4 5 3. To Access Array ElementsThe way there can be an array of integers or an array of floats, similarly there can be an array of pointers. Since a pointer variable always contains an address, an array of pointers would be nothing but a collection of addresses. The addresses present in the array of pointers can be addresses of isolated variables addresses of array elements or any other addresses that can be used to access array elements to print or perform operations on them. Internally the compiler uses pointers to access array elements. ExampleThe below example demonstrates that the compiler internally uses pointer arithmetic to access array elements. C++
Output
Value at arr[0]: 1 Value at arr[1]: 2 Value at arr[2]: 3 Value at arr[3]: 4 Value at arr[4]: 5 4. To Return Multiple ValuesPointers in C++ are used to return multiple values from a function. We can achieve this by passing memory addresses (or pointers) to the function, and then the modification of values can be done by the function at those addresses. Example C++
Output
Sum is: 7 Product is: 12 5. Creating Different Data StructuresDifferent data structures from arrays and linked lists to graphs and trees are implemented using pointers in C++. The following example demonstrates how to implement linked lists using pointers ExampleThe below example demonstrates the use of pointer to dynamically allocate memory for structure. C++
Output
Elements of the list are: 1 2 3 4 6. Achieving Runtime PolymorphismFunction pointers are used to achieve runtime polymorphism in C++. We generally create a pointer to the base class and based on the object it is pointing to, it finds the function to be executed. Not only that, pointers are used by the compiler to keep the track of virtual functions in the class. ExampleThe below example demonstrates the use of function pointers. C++
Output
Child display Base print ConclusionIn conclusion, pointers play an important role in C++. It offers various advantages like performance enhancement, reduced repetition, and an increase in flexibility to handle data structures. But with pros come cons so it’s important to use pointers carefully to avoid potential pitfalls like memory leaks and undefined behavior. |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |