![]() ![]() * C program to demonstrate pointer to constant Let us demonstrate pointer to a constant using an example. Syntax to declare pointer to constant const * Example to declare pointer to constant int num = 10 Ĭonst int * ptr_to_const = # // Pointer to constant Example program to use pointer to constant Whereas pointer to a constant cannot modify the value pointed by pointer, but can alter its value. You can, alter the value pointed by pointer, but cannot alter pointer value. A constant pointer can only point to single object throughout the program. Note: There is a minor difference between constant pointer and pointer to constant. You can modify pointer value, but you cannot modify the value pointed by pointer. Pointer to constant is a pointer that restricts modification of value pointed by the pointer. constantpointer.c: In function 'main':Ĭonstantpointer.c:16:15: error: assignment of read-only variable 'const_ptr' On compilation it generates following error message. The last two printf() statements are used to test value of num1.The statement will generate compilation error, since a constant pointer can only point to single object throughout the program. Next we tried re-assignment of constant pointer i.e.The statement *const_ptr = 10 assigns 10 to num1.First, we declared two integer variable num1, num2 and an integer constant pointer const_ptr that points to num1. ![]() Modification of pointer value is not allowed Modification of value pointed by pointer is allowed * C program to demonstrate constant pointer Let us write an example program to demonstrate constant pointer in C.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |