Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to use const with Pointers in C++?
Next article icon

How to use const with Pointers in C++?

Last Updated : 16 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers in C++.

const Qualifier with Pointers in C++

In C++, the const keyword can be used with pointers in different ways to specify whether the pointer itself, the data it points to, or both should be constant.

1. Pointer to Constant Data

Declaring the pointer to constant data ensures that the data pointed to by the pointer cannot be changed through this pointer, we can change the pointer to point to a different location, but cannot change the data at the location it points to.

Syntax to Declare Pointer to Constant Data

To make the data pointed by the pointer constant, use the below syntax:

const dataType* pointerName;

Example of Pointer to Constant

C++
// C++ program to demonstrate to use pointer to const data #include <iostream> using namespace std;  int main() {     int value = 5;     // creating a pointer to constant     const int* ptr = &value;      int anotherValue = 10;     cout << "Value: " << *ptr << endl;     //*ptr = 10; // Error: cannot modify the value pointed     // by ptr      ptr = &anotherValue; // Allowed: pointer can be                          // reassigned     cout << "Another Value: " << *ptr << endl; } 

Output
Value: 5  Another Value: 10  

2. Constant Pointer

Declaring a constant pointer means, the pointer itself is constant and we cannot change what it points to after its initialization, but we can modify the data at the location it points to.

Syntax to Declare Constant Pointer

To make the pointer itself constant, use the below syntax:

dataType* const pointerName;

Example of Constant Pointer

C++
// C++ program to demonstrate how to create a constant // pointer #include <iostream> using namespace std;  int main() {     int value = 5;     int anotherValue = 10;     int* const ptr = &value;     cout << "Initial Value: " << *ptr << endl;     *ptr         = 10; // Allowed: modifying the value pointed by ptr     cout << "Modified Value: " << *ptr << endl;     // ptr = &anotherValue; // Error: ptr is a const pointer     // and cannot be reassigned } 

Output
Initial Value: 5  Modified Value: 10  

3. Constant Pointer to Constant Data

Declaring constant pointer to constant data combines the restrictions of the previous two i.e. neither the pointer can point to a different address, nor the data it points to can be changed through it.

Syntax to Declare Constant Pointer to Constant Data

To make both the pointer and the data it points to constant, use the following syntax:

const dataType* const pointerName;

Example of Constant Pointer to Constant Data

C++
// C++ program to demonstrate how to use Constant Pointer to // Constant Data #include <iostream> using namespace std;  int main() {     int value = 5;     const int* const ptr = &value;     cout << "value: " << *ptr << endl;      //*ptr = 10;  // Error: cannot modify the value //     // pointed by ptr      // ptr = &anotherValue; // Error: ptr is a const pointer     // and cannot be reassigned } 

Output
value: 5  

Next Article
How to use const with Pointers in C++?

S

susobhanakhuli
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-pointer
  • C++-const keyword
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

    How to Use const_iterator with a Map in C++?
    In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{“apple”, 1}
    2 min read
    How to Traverse a Set with const_iterator in C++?
    In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in
    2 min read
    How to Traverse a List with const_iterator in C++?
    In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50}
    2 min read
    How to Use Const Keyword in C++?
    In C++, the const keyword is used to declare constants or unchangeable values. It indicates an immutable object that cannot be modified. The const keyword can be applied to variables, pointers, member functions, objects, and references. In this article, we will learn how to use the const keyword in
    4 min read
    How to Create Vectors of Unique Pointers in C++?
    In C++, the vector is defined as a dynamic array that can grow or shrink in size. Vectors of unique pointers are commonly used to manage the collections of dynamically allocated objects as they combine the dynamic resizing capability of vectors with the automatic memory management provided by unique
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences