View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

All About the Constant Pointer in C

Updated on 29/04/20253,970 Views

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.

What is Constant Pointer in C?

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:

Syntax of Constant Pointer in C

Let's look at the syntax to understand it better.

datatype *const pointer_name;

Here,

  • datatype is the type of data the pointer will point to.
  • *const means the pointer itself is constant.
  • pointer_name is the name of your pointer.

Example of Constant Pointer in C

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:

  • int *const ptr = &a; - This line declares ptr as a constant pointer to an integer, and it points to the address of a.
  • *ptr = 15; - This modifies the value at the memory address ptr points to. This is allowed because only the pointer is constant, not the data.
  • ptr = &b; - If you try to assign a new address to ptr, the compiler throws an error. A constant pointer must always point to the same address.

Note:

  • A Constant Pointer in C guarantees that the address stored in the pointer will not change.
  • You can still modify the value at that address unless the data is itself marked as constant.
  • Constant pointers are often used in embedded systems and critical applications to avoid accidental pointer reassignments.

How Does Constant Pointer Work in C?

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:

  • int *const ptr = &x; - This line creates a constant pointer ptr that points to the memory address of x.
  • printf("Initial value pointed by ptr: %d\n", *ptr); - This prints the value stored at the address ptr points to, which is 5.
  • *ptr = 25; - This changes the value of x to 25 using the constant pointer. It is allowed because the pointer’s target value can still be modified.
  • ptr = &y; - If you try to reassign ptr to point to y, the compiler will show an error. Constant pointers cannot change the address they store.

Must Explore: Static Function in C: Definition, Examples & Real-World Applications

Pointer to a Constant in C

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.

Need of Special Pointer Type for Const Variables?

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!

Some Examples of Pointers to a Constant in C

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:

  • const int *ptr = &a; - ptr is a pointer to a constant integer. You cannot modify the value at a through ptr.
  • *ptr = 15; - This operation is illegal because the pointer treats the pointed data as read-only.
  • ptr = &b; - This is allowed. The pointer can now point to the address of b.

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: 

  • void displayValue(const int *ptr); - The function accepts a pointer to a constant integer. It ensures that the function cannot modify the original variable.
  • *ptr = 100; -  Any attempt to change the value inside the function will cause a compile-time error.
  • displayValue(&num); - The function displays the value safely without altering it.

Points to Remember:

  • A Pointer to a Constant in C protects the data from being modified through the pointer.
  • The pointer itself is not constant. It can point to different addresses freely.
  • Using a pointer to a constant in function parameters helps maintain data integrity.
  • It is very useful when you want to pass large structures or arrays to functions without risking accidental changes.

Must Explore: Enumeration (or enum) in C article!

Constant Pointer to a Constant in C

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, 

  • datatype is the type of the value.
  • *const makes the pointer constant.
  • const before the datatype ensures the value pointed to is constant too.

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:

  • const int *const ptr = &a; - Here, ptr is a constant pointer to a constant integer. Both the address and the value are read-only through ptr.
  • *ptr = 35; - Modifying the value pointed to by ptr is not allowed and will result in a compile-time error.
  • ptr = &b; - Changing the pointer to point to another address is also not allowed and will cause a compile-time error.

Must Explore the strcat() Function in C: Syntax, Examples, and Common Errors article!

Conclusion

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.

FAQs

1. What is a Constant Pointer in C?

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.

2. Can a Constant Pointer change the value it points to?

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.

3. What is the syntax for declaring a Constant Pointer in C?

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.

4. What is a Pointer to a Constant in C?

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.

5. How is a Constant Pointer different from a Pointer to a Constant?

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.

6. Can we modify both the address and the value when using a Constant Pointer?

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.

7. What is a Constant Pointer to a Constant in C?

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.

8. Where are Constant Pointers commonly used in C programming?

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.

9. Can we use a Constant Pointer with functions in C?

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.

10. Will trying to reassign a Constant Pointer cause a runtime error?

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.

11. Is using a Constant Pointer in C helpful for optimizing code?

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.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

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.