EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Pointers in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Pointers in C++

Pointers in C++

What are the Pointers in C++?

Pointers the most powerful tool in c++, it helps the programmer to access and manipulate the memory directly.  For instance, when a variable is created, the job of the compiler is to do memory allocation to store the value of the variable. And this value is retrieved using the variable name assigned to the data. C++ has the compatibility to store and retrieve the data from the memory referring to the address of the memory location at which the data is stored. C++ even perform pointers on a pointer.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The general format of the pointer declaration is:

Data_type * pointer -variable-name

Note that the pointer variable should be preceded by an asterisk (*)

Example: int * xptr;

The variable xptr is a pointer to an integer. In general, pointer variables can point to integer variables, character variables, arrays, files, functions.

Why do we need Pointers in C++?

Dynamic memory allocation is made simple in C++ using pointers, the most prominent importance of pointers is they are much efficient in handling the different data types. They increase the execution speed when the function returns one value and also give hand in accessing a variable defined outside the function. The most common usage includes data management and accessing class member functions.

How to Create Pointers in C++?

Here are the following steps to create pointers in C++

Step #1 – Initialization of pointers

It is advisable to initialize pointer variables, as soon as they are declared. Since pointer variables store addresses, they can address any portion of the memory.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,828 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

int    *a; // pointer to an integer
double *da; // pointer to a double
float *fa; // pointer to afloat
char   *ch   // character pointer

Consider the following example:

int p, *pi; // This statement instructs the compiler to reserve a space for the variable p in memory to hold an integer value.

pi=&a;  // Assigns the address of the integer variable p to the pointer variable. For instance, if the address of p is 4581, then the value in the *pi will be equal to 4581.

Step #2 – Pointer void

Here the pointer variable is allowed to point any data type and this type is used in passing pointers to function that work independently of the data type that is pointed to.

Syntax: void * pointer variable;

Example:

#include<iostream.h>
#include <iostream>
using namespace std;
int main ()
{
int x, *iv;
float f, *fv;
void *vp;
x=3;
f=45.2;
iv=&x;
fv=&f;
vp=&x;
cout<< "the value pointed by iv is "<<*iv<< endl;
cout<< "The address of x is "<<iv <<endl;
cout<< "the value pointed by fv is "<<*fv<< endl;
cout<< "The address of f is "<<fv <<endl;
cout<< "The address of x is "<<vp <<endl;
vp= &f;
cout<< "the address of f is "<<vp<<endl;
}

Output:

Pointers in C++ 1

Step #3 – Pointers Arithmetic Operations in C++

Pointer arithmetic is performed with arrays. The following operations can be performed on pointers. They are:

  • Increment (++)
  • Decrement (–)
  • Pointer addition
  • Pointer subtraction

When we add 1 to the pointer it specifies adding the size of the pointer pointing at.

The below program specifies pointer arithmetic it works until it gets at the end of the array.

#include <iostream>
#include <string>
using namespace std;
void pointerarithmetic(int a[], int size)
{
int *e, *t; //Declaring  two int pointers variables
e = a; //assigning e to point the arrays initial element a[0] t = a + size; // assigning variable t to the array last element
while(e != t)
{
cout << *e << endl; //displays the e
e++; // incrementing ( next element)
}
}
int main()
{
int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
pointerarithmetic (a, 20);
return 0;
}

Output:

Pointers in C++ 2

Step #4 – Pointer to Pointer

float **fpp;

It denotes two levels of pointers (Multiple indirections). It’s a variable that points to another pointer further points to an object specified in a memory location. For example, fpp be a float pointer currently pointing to memory address 2001, the size of the float is 8 bytes, then by

fpp++;

indicates fpp pointing to address 2009. Similarly, when the variable is decremented by 1 it will point to the previous location of its base type at address 1993.

Step #5 – Pointer to functions

When pointers are passed to a function as arguments, the data items associated with these pointers’ variable are altered within the function and then returned to the calling program, the changes will be retained in the calling program. When a pointer is passed as a parameter, the respective data items are altered globally from within the called function. The pointer is passed by reference. Functions can be performed in pointers in different ways:

  1. the function invoked by passing the reference
  2. The function invoked by passing a pointer

The function invoked by passing the reference

In this, the address is passed as an argument instead of values.

Example:

#include <iostream>
using namespace std;
void changefn(int*, int*);
int main()
{
int n = 5, m = 6;
cout << "Before change" << endl;
cout << "n = " << n << endl;
cout << "m = " << m << endl;
changefn(&n, &m);
cout << "\nAfter change" << endl;
cout << "n = " << n << endl;
cout << "m = " << m << endl;
return 0;
}
void changefn(int* x1, int* x2) {
int s1;
s1 = *x1;
*x1 = *x2;
*x2 = s1;
}

Output:

Pointers in C++ 3

Conclusion

This article intended to refresh the knowledge about how to use pointers in C++ and their basic topics in a simple way with an example. Pointer also is known as locator reduces the code statement for higher performance. Pointers plays a vital role in implementing data structures like linked list and programming in the system level. They are the most preferable language in embedded systems as they are a good way to access memory directly using pointers.

Recommended Articles

This is a guide to Pointers in C++. Here we discuss the basic concept, why do we need it along with how to create pointers in C++ with appropriate examples and output. You may also look at the following articles to learn more –

  1. Pointers in Python
  2. Arrays in C++
  3. Star Patterns In c++
  4. Pointers in C#

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More