0

I have a huge byte array (char array) that I am using to write to another address. e.g.

char myBytes[] = { 0x43, 0x31, 0x63 };//just an example
(char*)0x123456 = &myBytes;

but the problem is that I get an error saying

"expression must be a modifiable lvalue"

I've tried some other stuff like

char myBytes[] = { 0x43, 0x31, 0x63 };//just an example
*(char*)0x123456 = myBytes;

But I get the same error. What am i doing wrong and what can I do get what I want accomplished?

8
  • 3
    memcpy((char*)0x123456, myBytes, sizeof(myBytes)); Bad idea, though... Commented Jan 13, 2017 at 20:48
  • (char*)0x123456 is a constant of type char *. You cannot assign to it any more than you can assign to the value 1. Commented Jan 13, 2017 at 20:48
  • @EugeneSh., why are you so sure it is a bad idea? Commented Jan 13, 2017 at 20:52
  • @SergeyA Working with explicit addresses in C or C++ is justified in a very special cases only, while I am not quite sure it is the case for the OP. And even in these cases there are often other ways. Commented Jan 13, 2017 at 20:54
  • @EugeneSh., apparently, you never worked with MCUs, did you? Commented Jan 17, 2017 at 14:37

1 Answer 1

3

You can't assign arrays with = in C++. The array name decays to a pointer when used like that, so you're trying to write the address of the array into the memory location. Use memcpy() to copy memory.

memcpy((void*)0x123456, (void*)mBytes, sizeof(mBytes));
Sign up to request clarification or add additional context in comments.

8 Comments

Not only does the array name decay to a pointer, any array value decays to a pointer when evaluated. Thus, you cannot even do tricks such as *(char(*)[3]) myBytes to get a handle on an array -- the array value resulting from that dereference just decays back to a pointer.
@JohnBollinger the "decay" does not happen on every evaluation. It only happens when the context demands a decay (which is most contexts, but not all). For example in &myBytes there is no decay, the address of the array results. And in your example (which is the same as *&myBytes), the & operator could be applied to the result.
@M.M, it is a fine point, to be sure, but evaluating the expression &myBytes, does not involve evaluating the array designated by myBytes, therefore that array does not decay to a pointer. This (and my earlier comment) are consistent with what you've said. I like this way of thinking about it because it has the advantage of requiring fewer exceptions, but it is admittedly a bit subtle.
@JohnBollinger & evaluates its operand. Every operand is evaluated unless the standard specifically says it is an unevaluated operand. Which is true for sizeof but not for &. Consider the code &foo() ; if & did not evaluate its operand then the function would not be called.
@M.M, good point, and I see that this is an area where C++ and C differ. In C++, the result of evaluating a function call expression is an lvalue under some (but by no means all) circumstances. As far as I can determine, the result of evaluating a C function call is never an lvalue. When it is not an lvalue, the result of a function call expression is not an allowed operand for the & operator in either language.
|

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.