Back to C Programming
2026-07-135 min read

Constant Pointer to Variable

Learn Constant Pointer to Variable step by step with clear examples and exercises.

Why This Matters

In this lesson, we will delve into understanding constant pointers and how they can be used to point to variables in C programming. This topic is crucial for anyone preparing for interviews or real-world coding scenarios as it helps prevent accidental modifications of variables and enhances code readability.

Prerequisites

To fully grasp the concept of constant pointers, you should have a solid understanding of:

  1. Pointers in C
  2. Variables and their types
  3. Constants and their usage
  4. Basic syntax and data structures in C

Core Concept

A pointer is a variable that stores the memory address of another variable. In C, we can declare pointers using the * symbol. A constant pointer, on the other hand, is a pointer that cannot be changed during runtime. To create a constant pointer, we prefix the pointer declaration with the const keyword.

const int *ptr; // Declaring a constant pointer to an integer

In this example, ptr is a constant pointer to an integer. It can only point to an integer variable that has been declared as const. If we try to change the value of the integer through the constant pointer, the compiler will throw an error.

Now, let's see how we can use a constant pointer to point to a non-constant variable.

#include <stdio.h>

int main() {
int num = 10; // Non-constant integer variable
const int *ptr = &num; // Creating a constant pointer to the non-constant variable

printf("The value of num is: %d\n", num);
printf("The address of num is: %p\n", &num);
printf("The value pointed by ptr is: %d\n", *ptr);

// Trying to change the value through a constant pointer will result in an error
// *ptr = 20; // Compiler Error: assignment of read-only variable 'num'

return 0;
}

In this code, we have declared a non-constant integer num. We then create a constant pointer ptr that points to the memory address of num. We can print the value of num, its memory address, and the value pointed by ptr. However, attempting to change the value through the constant pointer will result in a compiler error.

Worked Example

Let's consider a scenario where we have an array of integers and want to create a constant pointer that points to the first element of the array.

#include <stdio.h>

int main() {
const int arr[] = {1, 2, 3, 4, 5}; // Array of integers with read-only elements
const int *ptr = &arr[0]; // Creating a constant pointer to the first element of the array

printf("The value pointed by ptr is: %d\n", *ptr);
printf("The address of arr[0] is: %p\n", &arr[0]);
printf("The address pointed by ptr is: %p\n", ptr);

// Trying to change the value through a constant pointer will result in an error
// *ptr = 10; // Compiler Error: assignment of read-only variable 'arr'

return 0;
}

In this example, we have created an array arr with read-only elements. We then create a constant pointer ptr that points to the first element of the array. We can print the value pointed by ptr, the value of the first element in the array, and the memory address pointed by ptr. Attempting to change the value through the constant pointer will result in a compiler error.

Common Mistakes

  1. Forgetting the const keyword while declaring a constant pointer: If you forget to prefix the pointer declaration with the const keyword, you will end up with a regular pointer that can be modified during runtime.
int *ptr; // Regular pointer, not a constant pointer
  1. Trying to modify a variable through a constant pointer: Attempting to change the value of a variable through a constant pointer will result in a compiler error. Remember that constant pointers are read-only and cannot be used to modify the variables they point to.
const int *ptr = &num; // Constant pointer
*ptr = 20; // Compiler Error: assignment of read-only variable 'num'
  1. Using a constant pointer to point to a non-constant array: Although it is possible to create a constant pointer that points to an element in a non-constant array, the entire array remains modifiable. However, if you try to change the value of the element through the constant pointer, the compiler will throw an error.
int arr[] = {1, 2, 3}; // Non-constant array
const int *ptr = &arr[0]; // Constant pointer pointing to the first element of the non-constant array
*ptr = 10; // Compiler Error: assignment of read-only variable 'arr'

Practice Questions

  1. Write a program that declares an integer array and creates a constant pointer to the second element of the array. Print the value of the second element and its memory address using both the array notation and the constant pointer.
  1. Modify the previous program to change the value of the second element through a non-constant pointer and print the updated value using both the array notation and the constant pointer.

FAQ

  1. Can I create a constant pointer that points to a non-constant variable?

Yes, you can create a constant pointer that points to a non-constant variable. However, you cannot modify the value of the variable through the constant pointer.

  1. What happens if I try to change the value of a variable through a constant pointer in C?

If you try to change the value of a variable through a constant pointer in C, the compiler will throw an error because constant pointers are read-only and cannot be used to modify variables.

  1. Can I create a constant pointer that points to a non-constant array?

Yes, you can create a constant pointer that points to an element of a non-constant array. However, the entire array remains modifiable, and attempting to change the value of the element through the constant pointer will result in a compiler error.