what does (int*)p->m exactly means?

Hi, I was having an exercise to fully parenthesize some expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct X {
	int m;
};

int main() {

	int* x[10];
	int a = 2, i = 7;
	X d{ 4 };
	X *p = &d;
//expressions 
	*(p++);
	*(--p);
	(++a)--;
	(int*)p-> m;
	(*p).m;
	*(x[i]);

}


What exactly code on line 15 means? What does (int*) before expression stands for?
Last edited on
(int*) is an old, C style instruction that the object (in this case, the int p->m) be cast to an int* (i.e. create an int*, and load it with the value found in the int p->m).

This is bad in many, many ways. C++ gives improved ways to take an object of one type and turn it into another ( static_cast, dynamic_cast, const_cast, reinterpret_cast) that give you some safety and error checking as well, but any time you find yourself having to take an object of one type and pretend (or insist) it's actually something else, you should sit back and wonder how things got to that point and where it all went so wrong.

In this case, I can imagine what's meant; take an int value, such as 0x12341234, and then insist to the compiler that it be turned into a pointer, so that you have a pointer that is pointing at memory location 0x12341234. This is awful (and incidentally is the source of a common bug when old 32bit code was recompiled on a 64bit machine). Don't ever do it.
Thank you very much man, much appreciated :)
Topic archived. No new replies allowed.