Tuesday, August 11, 2015

const pointer to const data in c++

A pointer is a variable and it has got a value as other variable types. A pointer can be declared as a constant.

It is allowed to dereference a constant pointer and assign a new value to the pointed address under suitable conditions.

1-) const pointer to data :

In the following function "constPtrToData()" ptr is initially pointing to the address of the val1 which is holding the value of 10. Later constant pointer ptr is dereferenced and the value at the pointed address changed.

Therefore, the value of val1 and val2 variables becomes the same at the end of the "constPtrToData()" function call.

However, it is not allowed to directly change the address where the constant pointer points to.

If the constant pointer is assigned to the address of the second variable then a compile time error arises.

If you try to assign a new address to the const ptr then the following compile-time error is generated :

error: assignment of read-only variable 'ptr'

This means that you can not change the address assigned to the const pointer.

Terminal output of the "constPtrToData()" function shows that the initial address const ptr holds does not change, but the value stored at this address changes.


#include <iostream>
using namespace std;

void constPtrToData()
{
    //not allowed to change where the ptr points to
    int val1 = 10, val2 = 20;
    int *const ptr = &val1;

    cout << "\n";
    cout << "ptr value = " << *ptr << " address = "<< ptr << "\n";

    *ptr = val2;
    cout << "\n";
    cout << "ptr value = " << *ptr << " address = "<< ptr << "\n";
    cout << "\n";
    cout << "val1 value = " << val1 << " val2 value = " << val2 << "\n";

    // can not assign to constant ptr a new address
    // ptr = &val2;  // error: assignment of read-only variable 'ptr'
}

int main()
{
    constPtrToData();
    cout << "\n";

    return 0;
}



2-) pointer to const data

A pointer can point to constant data. At this time, the address that the pointer points to can be changed but the value can not.

Following compile-time error generated when you try to dereference a pointer which points to const data.

error: assignment of read-only location '* ptr2' 

Main aim of using pointer to const data is to protect the data being pointed to instead of the address of the pointer itself.

In the following "ptrToConstantData()" function, ptr2 is declared as a normal pointer which points to constant data.

For this case, it is allowed to change the address which pointer points to.


#include <iostream>
using namespace std;


void ptrToConstantData()
{
    //protect the value of the variable pointed to
    double val3 = 32, val4 = 45;
    const double *ptr2 = &val3;
    cout << "\n";
    cout << "ptr2 value = " << *ptr2 << " address = " << ptr2 << "\n";

    // can not change the value at the address ptr2 points to
    // *ptr2 = val4;   error: assignment of read-only location '* ptr2'

    // allowed to change the address of the ptr2 points to
    ptr2 = &val4;
    cout << "\n";
    cout << "ptr2 value = " << *ptr2 << " address = " << ptr2 << "\n";
    cout << "\n";
    cout << "val3 value = " << val3 << " val4 value = " << val4 << "\n";
}

int main()
{
    ptrToConstantData();
    cout << "\n";

    return 0;
}


Output of the function call is :



3-) const pointer to const data

Declaring a constant pointer which is pointing to constant data means that you are aiming to protect both the pointer and the data at the pointed address from being modified.

In this case, if you try to change the address of the pointer or dereference the pointer following error messages generated respectively :

error: assignment of read-only variable 'ptr3'

error: assignment of read-only location '*(const int*)ptr3'

Following "constPtrToConstData()" declares ptr3 as a const pointer to const int .


#include <iostream>

using namespace std;


void constPtrToConstData()
{
    //protect both pointer and the data from being modified
    int val1 = 10, val2 = 20;
    const int *const ptr3 = &val1;

    cout << *ptr3 << "\n";

    // ptr3 = &val2;  // error: assignment of read-only variable 'ptr3'
    // *ptr3 = val2;  // error: assignment of read-only location '*(const int*)ptr3'
}

int main()
{    
    constPtrToConstData();
    cout << "\n";

    return 0;
}

When the "constPtrToConstData()" function executed it just prints "10" as expected to the terminal.

No comments:

Post a Comment