noname
noname

Reputation: 199

const_cast on array giving error

I just wrote one trial program to see If I can change memory location pointed by an array using const_cast. I know that array const pointers but just wanted to see if it is allowed and I wrote below program

#include <iostream>

using namespace std;

    int main() {
        // your code goes here
        int arr[10], a ;  
        (const_cast<int*>(arr)) = &a;
        return 0;
    }             

I am getting below error message. What this error message means ? does this means I can take constness from array but not change it's value ?

prog.cpp: In function 'int main()':
prog.cpp:8:26: error: lvalue required as left operand of assignment
  (const_cast<int*>(arr)) = &a;
                          ^

Thanks

Upvotes: 0

Views: 434

Answers (2)

eerorika
eerorika

Reputation: 238311

to see If I can change memory location pointed by an array using const_cast

Arrays don't point to anything. They are a sequence of objects.

What this error message means ?

The value category of a cast expression to non-reference type is prvalue. You can only use a lvalue as the lefthand operand of assignment operator. Therefore you cannot use a cast expression as the lefthand operand in an assignment.

does this means I can take constness from array ...

The array isn't const in the first place, so using const_cast to remove constness doesn't make sense.

... but not change it's value ?

You can change the value of the objects in a non-const array, but arrays cannot be assigned to another, even if they are non-const. You cannot assign a pointer to an array variable either.

I can't just do arr=&a as arr being array is const pointer

No, an array is not a const pointer.

As I pointed out earlier, you can't do that because you cannot assign a pointer to an array variable.

Currently you're trying to change the type of int[10] to int*. They are unrelated types. const_cast can only be used to change constness or volatility. It cannot be used to change the type.


If you want to point to a memory location, then what you need is a pointer. To create a pointer variable, you cannot simply cast an array. Here is an example of how to define a pointer and initialize it to point to the first element of an array:

int arr[10];
int* pointer = arr;

And this is how you can change the memory location pointed by the pointer:

int arr2[10];
pointer = arr2;

Upvotes: 2

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

No, you cannot "change the memory location pointed by an array".

This is because in the context of your question, the "array" is not a discrete variable whose value can be changed. The name of the array is an alias for the first element in the actual array. It's not an actual, physical variable whose value can be changed. It's just an immutable label referencing a contiguous array of elements, of some length.

Consider this:

int a;

&a;

What you are asking for is, essentially, where you can change what &a points to. You can't, &a points to, well, a, and there's nothing you can do to change that.

Upvotes: 1

Related Questions