1

I'm reading a piece of code which does this

void *ptr_to_something = (void*)0x23; // I made this up, it's just a pointer to something void *addr; void *dst = (void*)(addr = ptr_to_something); // This is the line that confuses me 

it seems to assign a pointer to something to another pointer of the same thing. And that's okay.. but then the result is enclosed in parenthesis, cast to the same thing and somehow reassigned to a third pointer to the same thing.

Is this valid C++ at all? Is it guaranteed that assigning the result of an assignment yields the same assigned object?

2 Answers 2

5

It is valid in C++ to do:

int a, b, c; a = b = c = 1; 

So, therefore the result of an assignment is the value of that assignment.

What the cast is for, is a mystery*. Have you tried removing it, and does that generate a warning?

*not a mystery, just perhaps unnecessary.

Sign up to request clarification or add additional context in comments.

1 Comment

It's already a void* so just unnecessary. Also note that the assignment operator is on the precidence list and has right-to-left associativity en.cppreference.com/w/cpp/language/operator_precedence
2

This is valid C++. The choice to format the code this way is odd and not advised, but it's perfectly legal.

This code is equivalent to this (what I consider to be better written) code:

void *ptr_to_something = (void*)0x23; void *addr = ptr_to_something; void *dst = addr; 

1 Comment

Oh that line was just made up and entirely crap. The address in the real code is calculated elsewhere. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.