23 Jan C++ Pointers
The C++ Pointer is a variable that is used to store the memory address as its value. However, to get the memory address of a variable, use the & operator. This means a variable has a unique memory location. This memory location has its own address. To access this address, we use the ampersand i.e., the & operator.
In this lesson, we will discuss the two operators useful to understand the concept of Pointers in C++:
- The Address Operator: Access the address of a variable. Defined by &, the ampersand sign.
- The Indirection Operator: Access the value of an address. Defined by *, the asterisk sign.
Let us see an example to display the address of variables in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> using namespace std; int main() { int a; float b; char c; string d = "Demo"; cout <<"Address of variable a = "<<&a; cout <<"\nAddress of variable b = "<<&b; cout <<"\nAddress of variable c = "<<&c; cout <<"\nAddress of variable d = "<<&d; return 0; } |
Output
1 2 3 4 5 6 |
Address of variable a = 0x7ffe94acce58 Address of variable b = 0x7ffe94acce5c Address of variable c = 0x7ffe94acce83 Address of variable d = 0x7ffe94acce60 |
Pointers Syntax
The following is the syntax of Pointers in C++:
1 2 3 |
dataType *varName; |
Above, varName is the pointer variable, whereas datatype is the type.
Declare and create a Pointer
Follow the above syntax and let us see how we can create and declare pointers to different types.
Pointer to Integer
1 2 3 |
int* a; |
Pointer to Float
1 2 3 |
float* b; |
Pointer to Char
1 2 3 |
char* c |
Ways to Declare Pointers in C++
We can declare Pointers in any of the following three ways. Only a single way was demonstrated above:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int* a; int *a; int * a; float* b; float *b; float * b; char* c char *c char * c |
Pointer – Examples
After understanding the above concept, let us now move further and work around Pointer Examples in C++.
Pointer to Integer
The 1st example is to create a pointer variable and point to an integer variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; int main() { // Our integer variable i int i = 10; // A pointer variable j storing the address of the variable i int* j = &i; cout <<"Integer Value = "<<i; cout <<"\nThe memory address of the variable str = "<<&i; cout <<"\nDisplays the memory address of the variable i with the pointer = "<<j; return 0; } |
Output
1 2 3 4 5 |
Integer Value = 10 The memory address of the variable str = 0x7fff1fe951f4 Displays the memory address of the variable i with the pointer = 0x7fff1fe951f4 |
Pointer to String
The 2nd example is to create a pointer variable and point to a string variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> using namespace std; int main() { // Our string variable str string str = "Amit"; // A pointer variable p storing the address of the variable str string* p = &str; cout <<"String Value = "<<str; cout <<"\nThe memory address of the variable str = "<<&str; cout <<"\nDisplays the memory address of the variable str with the pointer = "<<p; return 0; } |
Output
1 2 3 4 5 |
String Value = Amit The memory address of the variable str = 0x7ffcda214900 Displays the memory address of the variable str with the pointer = 0x7ffcda214900 |
No Comments