Const Pointer in C

In C, a const pointer is a pointer that points to a memory location whose contents cannot be modified through that pointer. It is a way to enforce immutability or read-only access to data through a pointer. Here's how you declare and use a const pointer in C:

const int* ptr; // Declares a pointer to a constant integer

int x = 42;
ptr = &x; // Assign the address of x to the const pointer

// Accessing the value through the const pointer is allowed
int value = *ptr; // value = 42

// Attempting to modify the value through the const pointer is not allowed
// *ptr = 99; // Error: Assignment of read-only location

// You can change the pointer itself (i.e., make it point to another location)
int y = 55;
ptr = &y; // Legal

// You can also declare a constant pointer (pointer itself cannot be changed)
int z = 10;
const int* const constPtr = &z;

In the example above:

  • const int* ptr; declares a pointer named ptr to a constant integer. It means you can read the integer value through ptr, but you cannot modify it using *ptr.
  • ptr = &x; assigns the address of the integer x to the const pointer ptr. This is allowed because you're not modifying the data through the pointer; you're just pointing to a different location.
  • int value = *ptr; reads the value of x through the const pointer ptr. This is allowed because you're not modifying the data.
  • *ptr = 99; is not allowed because it attempts to modify the value through the const pointer, which is prohibited.
  • ptr = &y; is allowed because it changes the pointer itself to point to the address of y.
  • const int* const constPtr = &z; declares a constant pointer constPtr to a constant integer. It means you cannot modify the pointer or the data it points to.

Using const pointers can help prevent unintentional modification of data and make your code more robust, especially when you want to ensure the integrity of data that should not change through a particular pointer.

Let's illustrate the concept of const pointers in C with a real example. In this example, we'll create a const pointer to an integer and demonstrate how it enforces read-only access to the data it points to:

#include <stdio.h>

int main() {
// Declare an integer variable
int x = 42;

// Declare a const pointer to an integer
const int* ptr = &x;

// Access and print the value through the const pointer (read-only access)
printf("Value through const pointer: %d\n", *ptr);

// Attempt to modify the value through the const pointer (will result in an error)
// *ptr = 99; // Uncommenting this line will result in a compilation error

return 0;
}

In this example:

  1. We declare an integer variable x and assign it the value 42.
  2. We declare a const pointer to an integer const int* ptr and initialize it with the address of x using the address-of operator &. This pointer enforces read-only access to the integer pointed to.
  3. We access and print the value stored in the memory location pointed to by ptr using *ptr. This is allowed because it's a read operation.
  4. We attempt to modify the value through the const pointer by uncommenting the line *ptr = 99;. However, this line will result in a compilation error because const pointers prevent write (modification) access to the data.

By using a const pointer, you can ensure that the data it points to remains immutable, and any attempt to modify that data through the pointer will be caught by the compiler as an error. This is useful for cases where you want to protect data from unintended modifications.