Constant Pointer vs. Pointer to a Constant

Let's explore the differences between a pointer to a constant, a constant pointer, and a constant pointer to a constant in C with detailed code examples:

  1. Pointer to a Constant:

    • A pointer to a constant is a pointer that points to a constant value. The value being pointed to cannot be modified through the pointer.
    #include <stdio.h>

    int main() {
        const int num = 10;
        int anotherNum = 20;

        // Pointer to a constant
        const int *ptrToConst = &num;

        // Uncommenting the line below will result in a compilation error
        // *ptrToConst = 30;

        printf("Value pointed by ptrToConst: %d\n", *ptrToConst);

        return 0;
    }

In this example, ptrToConst is a pointer that points to a constant integer (num). You cannot modify the value through this pointer.

  1. Constant Pointer:

    • A constant pointer is a pointer whose address cannot be changed after initialization.
    #include <stdio.h>

    int main() {
        int num = 10;
        int anotherNum = 20;

        // Constant pointer
        int *const constPtr = &num;

        // Uncommenting the line below will result in a compilation error
        // constPtr = &anotherNum;

        printf("Value pointed by constPtr: %d\n", *constPtr);

        return 0;
    }

Here, constPtr is a constant pointer pointing to an integer. The pointer's address cannot be changed after initialization.

  1. Constant Pointer to a Constant:

    • A constant pointer to a constant is a pointer whose both the address and the value being pointed to cannot be changed after initialization.
    #include <stdio.h>

    int main() {
        const int num = 10;
        int anotherNum = 20;

        // Constant pointer to a constant
        const int *const constPtrToConst = &num;

        // Uncommenting either of the lines below will result in a compilation error
        // constPtrToConst = &anotherNum;
        // *constPtrToConst = 30;

        printf("Value pointed by constPtrToConst: %d\n", *constPtrToConst);

        return 0;
    }

In this case, constPtrToConst is a constant pointer pointing to a constant integer. Both the pointer's address and the value it points to cannot be changed after initialization.

Understanding and using these concepts correctly can help in creating more robust and maintainable code, especially when dealing with constant values and pointers in C.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.