C++ Pointers
Learning pointers in C++ is both simple and fun. Pointers can simplify the execution of some C++ programming tasks and enable others, such as dynamic memory allocation, which cannot be performed without them. Therefore, to become an excellent C++ programmer, learning pointers is essential.
As you know, every variable has a memory location, and each memory location defines an address that can be accessed using the ampersand (&) operator, representing an address in memory. Consider the following example, which outputs the addresses of defined variables:
Example
#include <iostream>
using namespace std;
int main ()
{
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6
Through the above example, we understand what a memory address is and how to access it. Next, let's see what a pointer is.
What is a Pointer?
A pointer is a variable whose value is the address of another variable, i.e., the direct address of a memory location. Like other variables or constants, you must declare a pointer before using it to store the address of another variable. The general form of a pointer variable declaration is:
type *var-name;
Here, type is the base type of the pointer, which must be a valid C++ data type, and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement, the asterisk is used to designate that the variable is a pointer. The following are valid pointer declarations:
int *ip; /* Pointer to an integer */
double *dp; /* Pointer to a double */
float *fp; /* Pointer to a float */
char *ch; /* Pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Using Pointers in C++
Several operations are commonly performed with pointers: defining a pointer variable, assigning the address of a variable to a pointer, and accessing the value at the address available in the pointer variable. These operations are performed using the unary operator * which returns the value of the variable located at the address specified by its operand. The following example involves these operations:
Example
#include <iostream>
using namespace std;
int main ()
{
int var = 20; // Declaration of the actual variable
int *ip; // Declaration of the pointer variable
ip = &var; // Store the address of var in the pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// Output the address stored in the pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// Access the value at the address in the pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Detailed Explanation of C++ Pointers
In C++, there are several concepts related to pointers that are simple yet important. Below are some key concepts that C++ programmers must be aware of:
Concept | Description |
---|---|
C++ Null Pointers | C++ supports null pointers. A NULL pointer is a constant with a value of zero defined in the standard library. |
C++ Pointer Arithmetic | Pointers can be subjected to arithmetic operations: ++, --, +, - |
C++ Pointers vs Arrays | There is a close relationship between pointers and arrays. |
C++ Array of Pointers | Can be defined to store arrays of pointers. |
C++ Pointer to Pointer | C++ allows pointers to pointers. |
C++ Passing Pointers to Functions | Pass parameters by reference or address, allowing the passed parameters to be changed in the calling function. |
C++ Returning Pointers from Functions | C++ allows functions to return pointers to local variables, static variables, and dynamically allocated memory. |