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

Output

Pointers Syntax

The following is the syntax of Pointers in C++:

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

Pointer to Float

Pointer to Char

Ways to Declare Pointers in C++

We can declare Pointers in any of the following three ways. Only a single way was demonstrated above:

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:

Output

Pointer to String

The 2nd example is to create a pointer variable and point to a string variable:

Output

 

 

C++ Destructors
C++ Overloading
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading