C

What is the difference between const pointer and pointer to const in C ?

In C programming, pointers are fundamental tools that allow direct manipulation of memory. However, when combined with the const qualifier, they can introduce subtle distinctions that often confuse both beginners and seasoned developers. At first glance, the terms const pointer and pointer to const might seem interchangeable, but they serve different purposes. This article explores these differences in depth. We’ll break down the syntax and semantics behind each type of declaration. By understanding these nuances, you can write more robust and safer code, making informed decisions about which approach best suits your programming needs.

In C, the placement of const in a pointer declaration affects whether the pointer itself is constant or whether the data it points to is constant. Let’s see the difference:

Pointer to Constant

const int *ptr

or

int const *ptr

In this case the pointer „points” to the integer variable which is a constant, i.e. it cannot be changed, but the pointer itself can be changed to point somewhere else, for example to another variable which is also a constant number. Let’s se an example:

const int *ptr;         // pointer ptr to constant integer
int x = 10, y = 11;     // some integer variables
ptr = &x;               // ptr points to the x variable
*ptr = 12;              // ERROR: Cannot modify constant value 
ptr = &y;               // OK: ptr can be change to point to the y variable

Because we declare *ptr as a pointer to „const int”, the value of x and y cannot be changed using this pointer reference, even if they were declared without const.

Constant Pointer

The second case is a constant pointer, which means that the pointer cannot be changed. It is fixed to a particular address and you cannot change it to point to another, but you can change the inside, directly or by pointer reference. Let’s se code snippet:

int x = 10, y = 11;       // integer variables
int *const ptr = &x;      // constant pointer to the address where x is located
*ptr = 11;                // you can modity the value of x by reference
x = 12;                   // or directly
ptr = &y;                 // you cannot change the pointer value (it's fixed to point to x) 

Summary

As you can see, there is a fundamental difference between a constant pointer and a pointer to a constant, although the syntax is very similar and it is easy to confuse them. What might help you distinguish them is to remember that everything to the left of the asterisk is the information about the variable stored at the address the pointer will point to, not the pointer itself. So if you see the keyword const before the asterisk, it means that this is a pointer to a constant variable. Otherwise, the pointer itself is constant, and you cannot change it to point to a different address.

Hello 👋 Nice to meet you.

Sign up here if you would like to be notified when new content is added to the blog.

We will not spam you. You will only receive important information, such as changes to the blog or new content.

Dodaj komentarz