Write a Program in C That Attempting To Modify a Constant Pointer To Non-Constant Data

// Attempting to modify a constant pointer to non-constant data.
#include <stdio.h>

int main( void )
{
   int x; // define x
   int y; // define y

   // ptr is a constant pointer to an integer that can be modified     
   // through ptr, but ptr always points to the same memory location
   int * const ptr = &x;                                               

   *ptr = 7; // allowed: *ptr is not const
   ptr = &y; // error: ptr is const; cannot assign new address
} // end main

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...