Site Tools

User Tools


Pointers

How to initialize a LPDWORD variable?

warning C4700: uninitialized local variable 'lpdwVolume' used

Incomplete code snippet:

LPDWORD lpdwVolume;  
 
// return value from auxGetVolume();
DWORD current_volume=0;
 
// This is usually zero
const UINT uDeviceID = 0;
 
current_volume = auxGetVolume(uDeviceID, lpdwVolume);

MS Visual Studio 2005 Express complained about this error when I tried to compile some test code which made use of the auxGetVolume WinAPI function.

I was a little confused about that 1) so I tried passing a regular DWORD variable.

No go (as one would expect in hindsight):

error C2664: 'DWORD (UINT,LPDWORD)' : cannot convert parameter 2 from 'DWORD' to 'LPDWORD'

I should have been passing the address of the DWORD to the function:

// return value from auxGetVolume();
DWORD current_volume=0;
 
// This is usually zero
const UINT uDeviceID = 0;
 
current_volume = auxGetVolume(uDeviceID, &lpdwVolume);

So, for this case, the address of the DWORD variable was passed to a function where a LPDWORD (long pointer to a DWORD) was expected. Chris Meech explained this to tguha:

In the example above, you give the address of the variable dwExitCode to the function, and that's similar to a pointer.

If a function expects a pointer to a specific data type, can the address of that data type be passed instead?

FIXME

Will there be cases where a pointer is necessary?

FIXME

Constant Pointers and Pointers to Constants

  • A pointer to a constant object
  • A constant pointer to an object
  • A constant pointer to a constant object

In the first situation, the object pointed to cannot be modified, but we can set the pointer to point to something else:

const char* pstring = "Some text";

In the second, the address stored in the pointer can't be changed, but the object pointed to can be:

char* const pstring = "Some text";

Finally, in the third situation, both the pointer and the object pointed to have been defined as constant, and therefore, neither can be changed:

const char* const pstring = "Some text";

My problem is I can't think of a good way to remember all of that without a “cheat sheet” around. The placement of const is not obvious to me.

Web References

1) and didn't bother to pick up my textbooks and review the material I read over in the past