C Programming Tutorial

C Programming Tutorial

Const Pointer in C

Hello Everyone, In this article, we will cover the concept of Const Pointer in C Programming. A constant pointer, or const pointer, is a type of pointer that points to a fixed memory address, meaning it cannot be changed to point to another address.

We will explore how to declare and use const pointers, and understand their significance in maintaining the integrity of data in our programs. Whether you’re new to C or have some experience, this guide will help you to understand basic concet of Const Pointer in C effectively. Let’s get started.

In C programming, a const pointer (or constant pointer) is a pointer that cannot change the memory address it points to once it is initialized. This means that once a const pointer is assigned an address, it cannot point to another address later in the program. However, the data at the pointed memory location can still be modified unless it’s a pointer to a constant data type. To declare a const pointer, you use the ‘const’ keyword before the ‘*’ symbol in the pointer declaration.

Here’s a basic example to help you understand const pointers:

#include <stdio.h>
int main() {
    int x = 10;
    int y = 20;

    int * const ptr = &x; // Declare a const pointer, pointing to the memory address of 'x'

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

    *ptr = 15; // Allowed: modifying the value at the memory address pointed by the const pointer
    printf("Value of x after modification: %d\\n", *ptr); // Output: Value of x after modification: 15

    // ptr = &y; // Not allowed: Changing the memory address of a const pointer (will result in a compilation error)

    return 0;
}

In this example, ptr is a const pointer that points to the memory address of the integer variable x. You can modify the value of x through the const pointer ptr, but you cannot change the memory address ptr points to. Attempting to do so, as shown in the commented-out line, will result in a compilation error.

Syntax and Declaration of Const Pointer in C

In C programming, when declaring a const pointer or a pointer to const data, you need to use the ‘const’ keyword along with the pointer declaration. Here’s the syntax and examples for declaring const pointers and pointers to const data:

  1. Const pointer (constant pointer): Syntax: type * const pointer_name; Example: int * const ptr1;
  2. Pointer to const data: Syntax: const type * pointer_name; Example: const int * ptr2;
  3. Const pointer to const data: Syntax: const type * const pointer_name; Example: const int * const ptr3;

Let’s go through each case with examples:

1. Const pointer (constant pointer):

    int x = 10;
    int * const ptr1 = &x; // Declare a const pointer
    
    *ptr1 = 20; // Allowed: Modify the value at the memory address pointed by the const pointer
    // ptr1 = &y; // Not allowed: Changing the memory address of a const pointer (will result in a compilation error)
    
    

    2. Pointer to const data:

      int x = 10;
      const int * ptr2 = &x; // Declare a pointer to const data
      
      // *ptr2 = 20; // Not allowed: Modifying the value at the memory address pointed by the pointer to const data (will result in a compilation error)
      int y = 20;
      ptr2 = &y; // Allowed: Changing the memory address of a pointer to const data
      

      3. Const pointer to const data:

        int x = 10;
        const int * const ptr3 = &x; // Declare a const pointer to const data
        
        // *ptr3 = 20; // Not allowed: Modifying the value at the memory address pointed by the const pointer to const data (will result in a compilation error)
        // ptr3 = &y; // Not allowed: Changing the memory address of a const pointer to const data (will result in a compilation error)
        
        Share This Article