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

// Attempting to modify data through a 
// non-constant pointer to constant data.
#include <stdio.h>
void f( const int *xPtr ); // prototype

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

   f( &y ); // f attempts illegal modification
} // end main

// xPtr cannot be used to modify the 
// value of the variable to which it points
void f( const int *xPtr )
{
   *xPtr = 100; // error: cannot modify a const object
} // end function f

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...