Constant Pointers & Pointer to Constant in C



In C, a pointer is a variable that stores the memory address of another variable, and the const keyword is used to define a variable or pointer whose value cannot be changed once initialized. When we combine pointers with const keyword, we can control two things −

  • Whether the address stored in the pointer can change.
  • Whether the value stored at that address can change.

In this chapter, we will look at the three main variations of constant pointers −

Constant Pointer

A constant pointer means the pointer itself is constant. Once it is initialized to point to a memory location, it cannot point to a different location, but the value stored at that location can still be changed.

Following is the syntax of a constant pointer

data_type *const pointer_name = &variable;

In this syntax, data_type is the type of data the pointer points to, *const makes the pointer itself constant, pointer_name is the pointer's name, and &variable assigns it the memory address of a variable.

Example of Constant Pointer

In this example, we declare a constant pointer ptr and initialize it with the address of variable x. Then, we change the value of x using ptr and we print its value.

#include <stdio.h>

int main() {
    int x = 10;
    int y = 20;

    int *const ptr = &x;  // constant pointer to int

    printf("Value of x: %d\n", *ptr);

    *ptr = 15;  // can change the value at address
    printf("Modified value of x: %d\n", *ptr);

    // ptr = &y;  // changing pointer address is not allowed
    return 0;
}

Shown below is the output of the above program, which shows that the pointer remains fixed to x, but the value of x can be updated.

Value of x: 10
Modified value of x: 15

Example of a Constant Pointer Error

Here's an example where we declare a constant pointer ptr and initialize it with the address of variable x. Then, we try to make it point to the address of variable y. This will give an error because a constant pointer cannot point to another memory location once it has been initialized.

#include <stdio.h>

int main() {
    int x = 10;
    int y = 20;

    int *const ptr = &x;  // constant pointer to int

    printf("Value of x: %d\n", *ptr);

    // Attempting to change the pointer to point to y
    ptr = &y; // cannot change the address of a constant pointer
    return 0;
}

You can see the error below, indicating that we cannot change the address of a constant pointer.

error: assignment of read-only variable 'ptr'

Pointer to Constant

A pointer to constant means the value it points to cannot be changed, but the pointer itself can point to different memory addresses (or variables).

Following is the syntax for pointer to constant

const data_type *pointer_name = &variable;
data_type const *pointer = &variable;

In this syntax, const data_type or data_type const means the pointer points to a constant value, pointer_name is the name of the pointer, and &variable assigns it the address of a variable.

Example of Pointer to Constant

In this example, we declare a pointer ptr that points to a constant value and assign it the address of variable a. Then, we make the pointer point to a different address of variable b and print its value.

#include <stdio.h>

int main() {
    int a = 5;
    int b = 30;

    const int *ptr = &a; // pointer to constant int

    printf("Value of a: %d\n", *ptr);

    // *ptr = 10; //we cannot modify value through pointer

    ptr = &b; // canging pointer address
    printf("Now pointing to b: %d\n", *ptr);
    return 0;
}

Following is the output of the above program, showing the same pointer pointing to different variables.

Value of a: 5
Now pointing to b: 30

Example of a Pointer to Constant Error

Here's an example where we declare a pointer to constant ptr and initialize it with the address of variable a. Then, we try to change the value of a through the pointer. This will give an error because a pointer to constant does not allow modifying the value it points to.

#include <stdio.h>

int main() {
    int a = 5;
    const int *ptr = &a; // pointer to constant int

    printf("Value of a: %d\n", *ptr);
    // we cnnot modify value through pointer to constant
    *ptr = 10; 
    return 0;
}

Below you can see the output, which shows an error indicating that we cannot modify the value through a pointer to constant.

error: assignment of read-only location '*ptr'

Constant Pointer to Constant

A constant pointer to a constant is a pointer that cannot change its memory address, and the value stored at that memory address also cannot be changed. Both actions are restricted, so we can only read the value, nothing else.

Following is the syntax for a constant pointer to a constant

const data_type *const pointer_name = &variable;

In this syntax −

  • const data_type indicates that the value at the memory location cannot be changed through the pointer.
  • *const pointer_name means the pointer itself cannot point to any other memory address after initialization.
  • &variable assigns the pointer to the memory address of the variable.

Example of Constant Pointer to Constant

In this example, we declare a constant pointer to constant ptr and assign it the memory address of variable a. We then print the value of a using the pointer.

#include <stdio.h>

int main() {
    int a = 10;

    const int *const ptr = &a;  // constant pointer to constant

    printf("Value of a: %d\n", *ptr);

    // *ptr = 15;  // we cannot modify value
    // ptr = &b;   // we annot change pointer location
    return 0;
}

Following is the output of the above program −

Value of a: 10

Example of Constant Pointer to Constant Error

In this example, we declare a constant pointer to constant ptr and initialize it with a variable a. Then, we try to modify the value through the pointer and also try to make the pointer point to another variable. Both operations are not allowed and will result in a compiler error.

#include <stdio.h>

int main() {
    int a = 10;
    int b = 20;

    const int *const ptr = &a;  // constant pointer to constant

    printf("Value of a: %d\n", *ptr);

    // *ptr = 15;  // we cnnot modify value
    // ptr = &b;   // we cannot change pointer address
    return 0;
}

Below you can see the output showing the errors −

Value of a: 10

Difference between Constant Pointer Types

The following table shows the differences between a constant pointer, a pointer to constant, and a constant pointer to constant.

Variation Definition Can Change Address? Can Change Value? Example Syntax
Constant Pointer A pointer whose address is fixed, but the value at that address can be modified. No Yes
int *const p = &x;
Pointer to Constant A pointer that can point to different addresses, but cannot modify the value at the pointed location. Yes No
const int *p = &x;
Constant Pointer to Constant A pointer whose address is fixed, and the value at that address cannot be modified. No No
const int *const p = &x;

Conclusion

In this chapter, we covered constant pointers and pointers to constant in C. Constant pointers fix the address but allow changing the value, pointers to constant allow changing the address but not the value, and constant pointers to constant restrict both.

Advertisements