hey guys,
i need help with understandind this code-actually just some parts of it. can you explain to me what the purpose of this is:
*(float*) variable = 34;
i can understand the variable = 34 part just not the *(float*) part. what does it do?
also what is the function of this: (DWORD*)
in this code
#define variable 0x00CB11C8
(some variable/pointer) = (DWORD*)variable

i think this code is from MFC (microsoft foundation class)
any help is appreciated

Dani AI

Generated

Quick summary for : that expression first converts an integer value (often a numeric address) into a pointer-to-float, then dereferences that pointer and stores 34 into the memory it points at. 's short explanation is essentially correct, and 's wording (“through it”) is a useful clarification: the code writes a float value into the memory location referred to by the cast pointer.

A clearer, modern-C++ equivalent (for illustration only — using a different example address) is:

#include <cstdint>

std::uintptr_t addr = 0x1000;          // example address, not the one in the thread
float *fp = reinterpret_cast<float*>(addr);
*fp = 34.0f;                           // stores 34.0 at that address (dangerous)

Important cautions and practical notes:

  • This is inherently unsafe unless the address is valid and writable in your process. Dereferencing a bad pointer causes crashes (access violations) and is undefined behavior in C++.
  • C-style casts hide intent. In C++ prefer reinterpret_cast for pointer/ integer conversions so the code reads more clearly.
  • Alignment and strict-aliasing rules matter: writing a float into memory that isn’t actually a float object can invoke undefined behavior. For type-punning or bit-level copies, prefer std::memcpy or C++20 std::bit_cast.
  • On Windows, DWORD is the WinAPI alias for an unsigned 32-bit integer (historically unsigned long); for portable code, use fixed-width types from <cstdint> (e.g., std::uint32_t).

Troubleshooting tips: run under a debugger, print or inspect the computed pointer before dereferencing, check page permissions (VirtualQuery/VirtualProtect on Windows) if you must access a specific address, and consider safer OS APIs (e.g., WriteProcessMemory) or writing a driver for hardware / low-level access rather than poking raw addresses from user code.

Recommended Answers

All 2 Replies

>>*(float*) variable = 34;

It is typcasting variable into a float *, then assigning the value 34 to it.

DWORD* is doing something similar to above. DWORD is defined by Microsoft's windows.h to be unsigned long;. So its the same as (unsigned long *)

I've studied MFC a lot and have never seen this: If it exists, please tell us where you found that. Maybe some very very old 16-bit code?

#define variable 0x00CB11C8
(some variable/pointer) = (DWORD*)variable

>>*(float*) variable = 34;

It is typcasting variable into a float *, then assigning the value 34 to it.

Correction: through it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.