0

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

6
  • just wanted to change memory location pointed by array using const_cast. I believe I can't just do arr=&a as arr being array is const pointer. I was hoping that by using const_cast I will be able to do above operation but this error message is something different. Commented Nov 17, 2015 at 12:04
  • An array does not point to anything, mainly because an array is not a pointer. Commented Nov 17, 2015 at 12:19
  • The array is not a constant pointer but the the array name is !! This means that you cannot change the location it (the array name) points at. Commented Nov 17, 2015 at 14:28
  • @sjsam array name is not a pointer either. In some contexts it does decay to a (non-const) pointer. The decayed pointer is not the array name. Commented Nov 17, 2015 at 14:39
  • 1
    @user2079303 : understood, taking it back.. But i have seen the statement "Array is constant pointer" in many printed materials. This statement is actually misguiding. Commented Nov 17, 2015 at 15:04

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

This example is classy and insightful. So +1 :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.