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++:

#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

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++:

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

int* a;

Pointer to Float

float* b;

Pointer to Char

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:

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:

#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

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:

#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

String Value = Amit
The memory address of the variable str = 0x7ffcda214900
Displays the memory address of the variable str with the pointer = 0x7ffcda214900

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More

C++ Destructors
C++ Overloading
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment