Constant pointer in c

02 Sep 2022 Balmiki Mandal 0 C Programming

Constant pointer in c programming language

There are two types of constant pointers:

A pointer to a constant:- this is a pointer that can be modified to point to a different memory location, but the memory location it points to cannot be modified. This is indicated in C by placing the const keyword before the data type being pointed to.

For example, the following code declares a pointer ptr that points to a constant integer:

c programming

const int num = 42;
const int *ptr = #

In this code, the variable num is a constant integer with a value of 42. The pointer ptr is declared as a constant pointer to an integer using the const keyword, which means that it cannot be used to modify the value of num.

 

A constant pointer to a non-constant

this is a pointer that cannot be modified to point to a different memory location, but the memory location it points to can be modified. This is indicated in C by placing the const keyword after the * (asterisk) symbol.

For example, the following code declares a constant pointer ptr to a non-constant integer:

C-programming
int num = 42;
int *const ptr = #

In this code, the variable num is a non-constant integer with a value of 42. The pointer ptr is declared as a constant pointer to an integer using the const keyword after the * symbol, which means that it cannot be modified to point to a different memory location.

Using constant pointers can be useful in situations where you want to prevent accidental modification of data in memory, or when you want to ensure that a pointer always points to the same memory location.

Top Resources

How to apply constant terms on the pointer in c

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.