What is Constant Pointer in C ?
In C, a constant pointer is a pointer that cannot be used to modify the value of the variable it points to. It allows you to declare a pointer that points to a constant value or a read-only memory location.
Syntax of Constant Pointer:
const data_type *pointer_name;
Here, const
is used to indicate that the pointer is constant, and data_type
is the data type of the variable being pointed to. The asterisk (*) denotes that pointer_name
is a pointer.
There are several reasons to use constant pointers in C:
- Preventing unintended modifications: By declaring a pointer as constant, you ensure that the value it points to cannot be modified accidentally, helping to maintain program integrity.
- Passing read-only data: Constant pointers can be used to pass read-only data to functions, ensuring that the data is not modified within the function.
Example of Constant Pointer in C
Here are two examples of using constant pointers in C:
Example 1:
#include <stdio.h>
int main() {
const int *ptr; // Constant pointer to an integer
int num = 10;
ptr = # // Assign the address of 'num' to 'ptr'
printf("Value of num: %d\n", *ptr);
// Attempt to modify the value pointed by 'ptr'
*ptr = 20; // Compilation error: assignment of read-only location
return 0;
}
Output:
Value of num: 10
Explanation:
In above example, a constant pointer ptr
is declared, which points to an integer num
. The value of num
is assigned to ptr
using the address-of operator (&
). The program prints the value of num
using the pointer (*ptr
). However, when an attempt is made to modify the value pointed to by ptr
(*ptr = 20
), a compilation error occurs since the pointer is constant.
Example 2:
#include <stdio.h>
void printString(const char *str) {
printf("%s\n", str);
}
int main() {
const char *message = "Hello, World!"; // Constant pointer to a string
printString(message);
return 0;
}
Output:
Hello, World!
Explanation:
In above example, a constant pointer message
is declared, which points to a string literal “Hello, World!”. The printString
function accepts a constant pointer to a character (const char *str
) and prints the string. The message
pointer is passed to the printString
function, and it successfully prints the string.
Different ways to use Constant Pointer in C
1. Constant Pointer to a Constant:
const int *const ptr;
This declaration indicates that both the pointer (ptr
) and the value it points to are constant.
2. Array of Constant Pointer:
const int *arr[5];
This declares an array of five constant pointers to integers.
Rules to be followed while using constant pointer
- A constant pointer cannot be used to modify the value it points to. Any attempt to modify a constant pointer results in a compilation error.
- A constant pointer can be used to point to a non-constant value, but the value cannot be modified using the constant pointer.
- A constant pointer can be assigned the address of a constant variable or a read-only memory location.
- A constant pointer can be used to pass read-only data to functions, ensuring that the data is not modified within the function.