const with Pointers

Four ways to pass a pointer to a function

Pointer Type

Data

non-constant

non-constant

non-constant

constant

constant

non-constant

constant

constant

(1) Nonconstant Pointer to Nonconstant data

#include <iostream>
using namespace::std;

void change(int * x2)
{
*x2=50;
}

void main()
{
int x=20;
cout<<" \n Value of x before calling change(int *) is : "<<x;

change(&x);

cout<<"\n\n Value of x after calling change(int *) is : "<<x<<endl;
}

(2) Nonconstant Pointer to Constant data

A nonconstant pointer to a constant data is a pointer that can modified to point to another data item, but the data which it points cannot be modified through that pointer.
Print a constant data
#include <iostream>
using namespace::std;

void print(const int * x2)
{ cout<<"x from print is "<<*x2<<endl; }

void main()
{
  int x=20;
  print(&x);
}
Try to modify a nonconstant pointer to a constant data
#include <iostream>
using namespace::std;

void change(const int * x2)
{*x2=50; // error : *x2 is const , cannot assign a new value }

int main()
{
 int x=20;
 cout<<" \n Value of x before calling change(int *) is : "<<x;
 change(&x);
 cout<<"\n\n Value of x after calling change(int *) is : "<<x<<endl;
}

(3) Constant Pointer to Nonconstant data

A constant pointer to a nonconstant data is a pointer that always points to same memory location ( can't be changed) but the data at that location can be modified through the pointer.

The default for an array name.

#include <iostream>
using namespace::std;

void main()
{
int x=30;
int * const xPtr=&x;    // Must be initialized when declared.
*xPtr = 50;

cout<<"x after changing it using *xPtr is : "<<x<<endl;
}
Try to modify a constant pointer to a nonconstant data
#include <iostream>
using namespace::std;

void main()
{
int x=30;
int y =90;
int * const xPtr=&x;

xPtr=&y; //error : xPtr is const , cannot assign a new value
}

(4) Constant Pointer to Constant data

A constant pointer to a constant data is a pointer that its address cannot be change and it cannot change the value that it is pointing to.

const int * const ptr  , read it from left to right , which is a constant pointer to a constant integer.  

#include <iostream>
using namespace::std;

void main()
{
int x=30;
const int * const xPtr=&x;


*xPtr=40;     // error : *xPtr is a const ; cannot assign a new value
 xPtr=&x;      // error : xPtr is a const ; cannot assign a new address
}
Example : Bubble sort
#include <iostream>
#include <iomanip>
using namespace::std;

void bubbleSort( int *, const int ); // prototype
void swap( int * const, int * const ); // prototype

int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

cout << "Data items in original order\n";

for ( int i = 0; i < arraySize; i++ )
cout << setw( 4 ) << a[ i ];

bubbleSort( a, arraySize ); // sort the array

cout << "\nData items in ascending order\n";

for ( int j = 0; j < arraySize; j++ )
cout << setw( 4 ) << a[ j ];

cout << endl;

return 0; // indicates successful termination
}
//---------------------------------------------------
// sort an array of integers using bubble sort algorithm
void bubbleSort( int *array, const int size )
{
// loop to control passes
for ( int pass = 0; pass < size - 1; pass++ )

// loop to control comparisons during each pass
for ( int k = 0; k < size - 1; k++ )

// swap adjacent elements if they are out of order
if ( array[ k ] > array[ k + 1 ] )
swap( &array[ k ], &array[ k + 1 ] );
}
//---------------------------------------------------
// swap values at memory locations to which element1Ptr and element2Ptr point
void swap( int * const element1Ptr, int * const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;

}