Today, our article will be about using pointers in C++. When it comes to pointers it is one of the most confusing topics among programmers. Most of the time programmers think of pointers as really complex concept but they are not. So, before going into details we should know what pointers are, what are their use cases and why them?
Pointers
A pointer is a variable that stores an object’s memory address. Using pointers in c++ and c, it is for three main reasons: allocating new objects on the heap, passing functions to other functions, and so on. to cycle through the elements of arrays or other data structures
Syntax: datatype* pointerVariableName = &variable;
Why use pointers?
- Pointers save memory.
- Because data changes with the address, that is, direct access to a memory location, execution time with pointers is faster.
- The pointers provide quick access to memory. The pointer both assigns and releases memory. As a result, pointer memory is dynamic in allocation.
- Data structures make use of pointers; for example, the use of two and three-dimensional arrays is with points.
- Pointers can access any type of array without considering the subscript range.
- For file handling, pointers have their application..
- Pointers are used to dynamically allocate memory.
- A pointer to a base class in C++ could be used to retrieve the object of a derived class. A pointer to a derived class, on the other hand, cannot access the object of a base class.
So, these are some benefits of using pointers, but as I have mentioned earlier a pointer is a variable and yes, we know variables are used to store some data in them. But in the case of pointers, the data itself is the address of some other variable. But from where do we can get the address of a variable. Well, it’s very easy for you can find the address of the variable using & the operator.
Syntax: &varName -> returns address of variable
I hope you all are familiar with variables and data types. A variable can be of a specific data type holding data of that data type. Similarly, pointers can be of different data types holding address of that variable. For example, if you have some variables for storing details of student say:
string name = "John Alex";
int age = 21;
double gpa = 3.5;
Their respective pointers would be
string* namePtr = &name;
int* agePtr = &age;
double* gpaPtr = &gpa;
Seems easy right. However, if you try to assign the address of some variable to a pointer which is not of the same data type, it yields an error.
As you can see the last line is highlighted with red color, which shows an error on this line. Now let’s print what do we have in pointers.
#include <iostream>
using namespace std;
int main() {
string name = "John Alex";
int age = 21;
double gpa = 3.5;
string* namePtr = &name;
int* agePtr = &age;
double* gpaPtr = &gpa;
cout << "\tName of student is " << name << " And pointer to it has address " << namePtr << "\n";
cout << "\tAge of student is " << age << " And pointer to it has address " << agePtr << "\n";
cout << "\tGpa of student is " << gpa << " And pointer to it has address " << gpaPtr << "\n";
return 0;
}
Cool, I believe you should have the basics of pointers now. If you still have trouble understanding the basics of them, please read them again and grasp the core concepts. With that said, let’s move to another important concept of pointers.
Referencing & Dereferencing pointers
In our above examples when we used & operator to store the address of a variable in the pointer, this sign is called a reference operator in C++. But what does it mean by dereferencing pointers, well let me ask a question from you? If we want to access the value of a variable whose address, we have stored in the pointer using a pointer, how can we? Well, if you think by using * operator then you are right. We normally used * operator for dereferencing a pointer. In the above example let’s print out values of all variables corresponding to student details using pointers.
#include <iostream>
using namespace std;
int main() {
string name = "John Alex";
int age = 21;
double gpa = 3.5;
string* namePtr = &name;
int* agePtr = &age;
double* gpaPtr = &gpa;
cout << "Student Name: " << *(namePtr) << "\n";
cout << "Student Age: " << *(agePtr) << "\n";
cout << "Student Gpa: " << *(gpaPtr) << "\n";
return 0;
}
Conclusion
If you have guessed the syntax of dereferencing pointer as *(pointerName) then you are right. In our next tutorials, we will talk about the concepts of pointers and arrays. So, Thanks for reading, and stay tuned with us for more such amazing tutorials.