Pointers in C++
In the C++ programming language, a pointer is like a variable that holds the specific address of another variable in memory. Like variables, pointers also have a data type. For example, a pointer of type character can hold the address of a variable of the type character. A pointer of integer type can hold the address of a variable of integer type.
Pointer is a type of symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also manipulate or create dynamic data structures.
Address in C++
When we create a variable in our code, it is assigned some space in the computer memory. Therefore, the value of this variable is stored in the assigned location.
Now, to know the location in the memory of the computer where the data is stored, C++ language provides the & (reference) operator. The reference operator returns the address that a variable.
For example, if z is a variable, &z returns the address of the variable.
Pointer Declaration Syntax
The declaration has the following syntax:
- The data type is the base type of the pointer.
- The variable_name should be the name of the pointer variable.
- The asterisk(*) used for pointer declaration is the same as the asterisk used to perform multiplication operations. It is the asterisk that denotes the variable as a pointer.
Here is an example of valid pointer declarations in C++:
int *z; // a pointer to integer double *z; // a pointer to double float *z; // a pointer to float char *z // a pointer to a character
Reference operator (&) and Deference operator (*)
The reference operator (&) returns the variable’s address.
The deference operator (*) helps us get the value that has been stored in a memory address.
If there is no exact address that is to be assigned to a pointer, then the pointer variable can be assigned a NULL value. It should be done declared during the declaration of the pointer. Such a pointer is known as a null pointer. The NULL pointer has value is zero.
using namespace std;
int *ip = NULL;
cout << "Value of ip: " << ip;
Pointers of Variables
Pointer variables point to a specific memory address in the memory pointed to by another variable. With C++ language, you can change data directly from the memory of the computer. The memory space can be assigned or re-assigned according to the user.
It can be declared as follows:
Application of Pointers
To pass arguments by reference. Passing by reference serves two purposes
(i) To modify variable of function in other- Arguments to function are passed by value, and any change done on the variables doesn’t change the value of the actual variables that are passed.
(ii) For efficiency purposes– Functions in C++ can return only one value. Further, all the variables declared in a function are allocated on the function call stack. As soon as the function returns, all the stack variables are destroyed.
Advantages of using Pointers
- Variables that hold the address of other variables are known as pointers.
- Multiple variables can be modified and returned by function using pointers.
- Memory can be dynamically allocated and de-allocated.
- Helps to Simplify the complexity of the code.
- The execution speed of a code improves.
- Each pointer has a valid data type.
- Is a symbolic representation of a memory address in the computer memory.
- Has call-by-reference feature that can create and manipulate dynamic data structures.
- If you want to assign the address of an array to a pointer, don’t use an ampersand (&).
- If there is no specific address to assign a pointer variable, assign it a NULL(zero).