Ad Code

Responsive Advertisement

const Pointer in C ? With example | Constant Pointers | Pointer to Constant

 const Pointer in C

Constant Pointers

A constant pointer in C cannot change the address of the variable to which it is pointing, i.e., the address will remain constant. Therefore, we can say that if a constant pointer is pointing to some variable, then it cannot point to any other variable.

const pointer is a pointer that points to a constant value. When you declare a pointer as const, it means that the value it points to cannot be modified through that pointer. This is a way to enforce immutability and improve code safety by preventing unintended changes to the data.

Syntax of Constant Pointer

c
const data_type *pointer_name;


Where:
const: The const keyword indicates that the pointer is constant, and its value cannot be changed.
data_type: The data type of the object to which the pointer points.
pointer_name: The name of the constant pointer.

For example, if you want to declare a constant pointer to an integer, you would write:

c
const int *ptr;

Declaration of a constant pointer is given below:

c
const int *ptr;


This declares a constant pointer ptr that points to an integer. This means that the memory location it points to cannot be changed through ptr, but you can modify the integer value it points to.

Let's understand the constant pointer through an example.

c
#include <stdio.h>

int main() {
int x = 10;
int y = 20;
const int *ptr = &x;

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

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

ptr = &y; // This is allowed, as ptr itself is not constant

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

return 0;
}


In this example,

1. we have a constant pointer ptr pointing to an integer x.

2. We print the value pointed to by ptr and then attempt to change the value through ptr (which is commented out because it would result in a compilation error).

3. After that, we make the pointer ptrpoint to the integery`, which is allowed because the pointer itself is not constant.

Output:
vbnet
Value pointed to by ptr: 10
Value pointed to by ptr: 20


Pointer to Constant

A "pointer to a constant" in C is a pointer that can point to a constant value, meaning it can point to a data item that cannot be modified through that pointer. You declare a pointer to a constant using the const keyword. Here's the syntax and an example:

Syntax:

c
const data_type *pointer_name;

Where:
1. const: The const keyword indicates that the data pointed to by the pointer is constant and cannot be modified through this pointer.
2. data_type: The data type of the constant data.
3. pointer_name: The name of the pointer.

Let's understand the constant pointer through an example.

c
#include <stdio.h>

int main() {
const int x = 10;
const int *ptr = &x;

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

// Uncommenting the line below will result in a compilation error
// *ptr = 20;

return 0;
}


In this example, 
we have a constant integer x, and we declare a pointer ptr to a constant integer. 
This means that ptr can point to a constant value (in this case, x), but you cannot modify the value of x through ptr.

If you try to uncomment the line that attempts to modify *ptr, it will result in a compilation error because you are trying to modify a constant value through a pointer to a constant.

Output :

vbnet
Value pointed to by ptr: 10

So, the program successfully prints the value pointed to by ptr, which is the constant integer x.

Constant Pointer to a Constant

A "constant pointer to a constant" in C is a pointer that points to a constant value and itself is constant, meaning both the pointer and the data it points to cannot be modified through that pointer. You declare a constant pointer to a constant using the const keyword. Here's the syntax and an example:

Syntax:

c
const data_type *const pointer_name;

Where:

1. const: The first const keyword indicates that the data pointed to by the pointer is constant and cannot be modified through this pointer.
2. data_type: The data type of the constant data.
3. *const: The second const keyword indicates that the pointer itself is constant and cannot be modified to point to a different location in memory.
4. pointer_name: The name of the constant pointer.

Example:

c
#include <stdio.h>

int main() {
const int x = 10;
const int *const ptr = &x;

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

// Uncommenting the line below will result in a compilation error
// *ptr = 20;
// Uncommenting the line below will also result in a compilation error
// ptr = &some_other_constant;

return 0;
}


In this example, 
we have a constant integer x, and we declare a constant pointer ptr to a constant integer. 
This means that ptr can only point to x, and you cannot modify either the value of x or make ptr point to another location in memory.

If you try to uncomment the lines that attempt to modify *ptr or change the value of ptr, they will result in compilation errors because you are trying to modify a constant value through a pointer to a constant or change the constant pointer itself.

Output (without the commented lines):

vbnet
Value pointed to by ptr: 10
So, the program successfully prints the value pointed to by ptr, which is the constant integer x.

Post a Comment

0 Comments