- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What do you mean by pointer to a constant in C language?
The value of the pointer address is constant that means we cannot change the value of the address that is pointed by the pointer.
A constant pointer is declared as follows −
Data_Type const* Pointer_Name;
For example, int const *p// pointer to const integer
Example
Following is the C program to illustrate a pointer to a constant −
#include<stdio.h> int main(void){ int var1 = 100; // pointer to constant integer const int* ptr = &var1; //try to modify the value of pointed address *ptr = 10; printf("%d
", *ptr); return 0; }
Output
When the above program is executed, it produces the following result −
Display error, trying to change the value of pointer to constant integer
Example
Following C program demonstrates what happens if we remove const −
#include<stdio.h> int main(void){ int var1 = 100; // removed the pointer to constant integer int* ptr = &var1; //try to modify the value of pointed address *ptr = 10; printf("%d
", *ptr); return 0; }
Output
When the above program is executed, it produces the following result −
10
Advertisements