Constant Pointer in C
×


Constant Pointer in C

314

 A constant pointer in C is a pointer whose memory location(address) cannot be changed once it is declared.

 The constant pointer points to the same memory location throughout life.

 The value stored at the memory location can still be modified.

 This detailed diagram represents a const pointer in C, pointing to a constant value.

 The pointer ptr is marked as constant using the const keyword, meaning its value (the memory address it holds) cannot be changed after initialization.

 ptr points to a specific memory address, 0x7fff.

 At the memory address 0x7fff, there's a constant value of 10.

 Once initialized, the pointer ptr cannot be reassigned to point to a different memory address, but it can still be used to access the constant value 10 stored at 0x7fff.

Syntax of Constant Pointer in C 

const int *ptr; // Constant pointer to an integer
If you want the data pointed to a pointer to constant. you can putthe 'const' keyword before the data type.

int *const ptr; // Pointer to a constant integer
Example:

 // Program for constant Pointer in C
#include<stdio.h>
int main() {
int A = 30; int B = 10; // Constant pointer to an integer const int *ptr = &A;

printf("Value of A: %d\n", *ptr);
A = 50;
printf("Value of A after Change: %d\n", *ptr);

return 0;
}
Output:

Value of A: 30
Value of A after Change: 50


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments