A constant pointer is a pointer that cannot change the address which it stores.
Syntax: datatype *const pointer_name;
Example:
int a=10,b=20;
int *const ptr;
ptr=&a;
ptr=&b; //here the address in ptr cannot be changed so not allowed .
Pointer to constant
Address in the pointer can be changed but the value of the data element which the pointer points cannot be changed.
Syntax: const datatype * pointer_name;
Example:
int a=10,b=20;
const int *ptr=&a;
*ptr=100; //here the value which ptr points to cannot changed so not allowed ptr=&b; //allowed
Pointers and Const
The previous two can be combined to form a constant pointer to constant
Syntax: const datatype *const pointer_name; Example:
const int * const ptr;
int a=10,b=20;
const int * const ptr=&a;
*ptr=100; //not allowed
ptr=&b;//not allowed
Here the value which ptr points to cannot be changed and also the address in ptr cannot be changed
Also Read
- Static and Dynamic Memory Allocation
- Memory Leak and Dangling Pointer
- Memory Allocation for 2D Array
- Dynamic Memory Allocation
- Some Pointer Declaration and their Meanings
- Function and pointers
- Initializing Pointer to Pointer
- Pointer to Pointer Multiple Indirection
- Relationship between Arrays and Pointers
- Pointer to Array
- Pointer Arithmetic
- Types of Pointers
- Illustrate Basic Pointer Use
- Using Pointer
- Intializing Pointer
- Declaring Pointer
- Application of Pointers
- Value Model VS Reference Model
- What is Pointer
Comments
Post a Comment
Please give us feedback through comments