Demonstrating the const type qualifier in parameter - C++ Function

C++ examples for Function:Function Parameter

Description

Demonstrating the const type qualifier in parameter

Demo Code

#include <iostream> 
using namespace std; 

void tryToModifyArray( const int [] ); // function prototype 

 int main() //from ww w . j av a  2s.  c o m
 { 
     int a[] = { 10, 20, 30 }; 

    tryToModifyArray( a ); 
    cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n'; 
 }

// "b" cannot be used to modify the original array "a" in main. 
void tryToModifyArray( const int b[] ) 
{ 
    //b[ 0 ] /= 2; // compilation error 
}

Result


Related Tutorials