For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
In C programming, a Constant Pointer in C refers to a pointer whose address (the memory location it points to) cannot be changed after initialization. Understanding constant pointers is important for managing memory safely and ensuring that critical data remains protected.
In this article, we will cover how a constant pointer works in C, the difference between a constant pointer and a pointer to a constant, and why a special pointer type is needed for constant variables. Apart from all this, we will walk through practical examples and also discuss the concept of a constant pointer to a constant before wrapping up with a quick summary.
Explore Online Software Engineering Courses from top universities.
A Constant Pointer in C is a type of pointer that always points to the same memory location after it is initialized. In simple words, once you assign an address to a constant pointer, you cannot change it to point somewhere else. However, you can modify the value stored at that address unless the pointed data is also marked const.
If you're new to C, consider going through our Introduction to C tutorial for better clarity before moving ahead.
Constant pointers are useful when you want to make sure that a pointer always refers to the same object. This feature adds safety to your C programs, especially when you work with critical variables.
Check out these courses:
Let's look at the syntax to understand it better.
datatype *const pointer_name;
Here,
Here is a simple code example to demonstrate a constant pointer:
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
// Declaring a constant pointer to an integer
int *const ptr = &a;
printf("Value pointed by ptr: %d\n", *ptr);
// Changing the value at the address (allowed)
*ptr = 15;
printf("New value pointed by ptr after modification: %d\n", *ptr);
// Trying to make ptr point to b (Not allowed)
// ptr = &b; // Uncommenting this line will cause a compile-time error
return 0;
}
Output:
Value pointed by ptr: 10
New value pointed by ptr after modification: 15
Explanation:
Note:
In C programming, a Constant Pointer in C works by locking the pointer to a specific memory address. Once you initialize a constant pointer with a variable’s address, you cannot make it point to another address later. However, you still have full control to modify the value stored at that location.
This behavior is important when you want to maintain stable memory references while still updating the data. To see how a const pointer actually works in C, let’s look at an example.
Example Showing How Const Pointer Works:
#include <stdio.h>
int main() {
int x = 5;
int y = 8;
// Declaring a constant pointer to an integer
int *const ptr = &x;
// Printing the initial value
printf("Initial value pointed by ptr: %d\n", *ptr);
// Modifying the value at the address (allowed)
*ptr = 25;
printf("Value after modification: %d\n", *ptr);
// Attempting to change the pointer address (not allowed)
// ptr = &y; // Uncommenting this line will cause a compile-time error
return 0;
}
Output:
Initial value pointed by ptr: 5
Value after modification: 25
Explanation:
Must Explore: Static Function in C: Definition, Examples & Real-World Applications
In C programming, a Pointer to a Constant means you cannot change the value at the memory location through the pointer. However, the pointer itself can point to different memory locations if needed. This setup helps you protect important data while still allowing flexibility in pointer movement.
Let's examine why it is not desirable to save the address of a const variable using a regular pointer. Consider the following instance:
#include <stdio.h>
int main()
{
const int x = 10;
int *ptr = &x;
*ptr = 50;
printf("Value of 'x' is %d", x);
return 0;
}
Output:
main.c:6:20: warning: The initialization discards the 'const' qualifier from the pointer target type [-Wdiscarded-qualifiers]
6 | int *ptr = &a;
| ^
The value of 'a' is 50
As we can see from the result above, even though an is defined as a const variable, its value has changed from 10 to 50 despite the compiler issuing a warning. This occurs because anytime a regular pointer, like ptr, attempts to change the value of a const variable, the C compiler for Mac momentarily discards the constant attribute that was assigned to the variable. This is not advised since it might result in security holes and undermine the goal of making the variable constant.
As a result, in order to point to constant variables in C, we must use a distinct pointer-type syntax.
Also read the strlen() Function in C article!
Example 1: Basic Pointer to a Constant
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
// Declaring a pointer to a constant integer
const int *ptr = &a;
printf("Value pointed by ptr: %d\n", *ptr);
// Trying to modify the value through the pointer (not allowed)
// *ptr = 15; // Uncommenting this will cause a compile-time error
// Changing the pointer to point to another address (allowed)
ptr = &b;
printf("Now ptr points to: %d\n", *ptr);
return 0;
}
Output:
Value pointed by ptr: 10
Now ptr points to: 20
Explanation:
Also Read: Strcpy in C: How to Copy Strings with Examples article!
Example 2: Pointer to Constant with Functions
Sometimes, you may want to pass a pointer to a constant to a function to avoid accidental modification.
#include <stdio.h>
// Function that takes a pointer to a constant integer
void displayValue(const int *ptr) {
printf("Value received: %d\n", *ptr);
// Trying to modify the value will cause an error
// *ptr = 100; // Not allowed
}
int main() {
int num = 50;
// Calling the function with the address of num
displayValue(&num);
return 0;
}
Output:
Value received: 50
Explanation:
Points to Remember:
Must Explore: Enumeration (or enum) in C article!
In C programming, a Constant Pointer to a Constant means two things: you cannot change the address stored in the pointer, and you cannot modify the value at that address through the pointer. In short, both the pointer and the value it points to are protected from modification. The general syntax for a Constant Pointer to a Constant is:
const datatype *const pointer_name = &variable;
Where,
This setup is very useful when you want maximum safety. It ensures that neither the pointer nor the data it points to will be accidentally changed. Let's dive into an example to make it even clearer.
Example of Constant Pointer to a Constant
#include <stdio.h>
int main() {
int a = 30;
int b = 40;
// Declaring a constant pointer to a constant integer
const int *const ptr = &a;
// Printing the initial value
printf("Value pointed by ptr: %d\n", *ptr);
// Trying to modify the value through the pointer (not allowed)
// *ptr = 35; // Uncommenting this line will cause a compile-time error
// Trying to change the pointer address (not allowed)
// ptr = &b; // Uncommenting this line will also cause a compile-time error
return 0;
}
Output:
Value pointed by ptr: 30
Explanation:
Must Explore the strcat() Function in C: Syntax, Examples, and Common Errors article!
Understanding how a Constant Pointer in C works is essential for writing safe and reliable programs. Constant pointers help you fix a pointer to a memory address, while pointers to constants protect the data itself. When combined, a constant pointer to a constant provides full protection — locking both the address and the value.
In this article, we explored how constant pointers function, why pointers to constants are needed, and how a constant pointer to a constant ensures maximum safety. We also covered multiple examples to show their real-world use. Mastering these concepts will make your C programming skills stronger, especially when working with complex memory operations and critical systems.
A Constant Pointer in C is a pointer whose address cannot change after initialization. Once assigned, it always points to the same memory location. However, the value stored at that location can still be modified.
Yes, a constant pointer can change the value at the memory location it points to. Only the address it stores is constant. It treats the pointed data as normal unless the data is separately marked const.
To declare a Constant Pointer in C, use the syntax datatype *const pointer_name;. Here, const makes the pointer constant, meaning you cannot assign it a new address after its initial assignment.
A Pointer to a Constant in C allows you to point to a value that should not be modified through the pointer. However, you can make the pointer point to different memory locations during the program's execution.
A constant pointer locks the address but allows data modification. A pointer to a constant locks the data but allows address changes. They serve different needs for memory safety and variable protection in C programs.
No, with a constant pointer, you cannot change the address after initialization. But you can still modify the value at the pointed location unless the data itself is declared as const separately.
A Constant Pointer to a Constant in C means you cannot change either the pointer's address or the value it points to. Both are treated as constant, making the pointer completely read-only and extremely safe.
Constant pointers are widely used in embedded systems, operating system kernels, and device drivers. They ensure that critical variables are not accidentally redirected, improving code stability, memory safety, and reducing programming errors.
Yes, you can pass constant pointers to functions to make sure the pointer cannot point to a different variable inside the function. This protects important references and keeps the code more predictable and safe.
No, trying to reassign a constant pointer will cause a compile-time error, not a runtime error. The compiler will immediately detect the issue and stop the code from compiling, thus preventing any memory corruption.
Yes, using a Constant Pointer in C helps optimize code by preventing unintended memory operations. It also allows compilers to perform better optimizations because they know the pointer address will never change during execution.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Disclaimer
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.