Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

I would like to print address of pointer in order to see if is changing during program run ( exe life ).

I wrote:

C++
char	DebugString[100];
uint8	*m_pPointer;
void *val1 = pPointer;
sprintf_s(DebugString,"pPointer: %d\n",val1);
OutputDebugString(DebugString);



Is it the right way to print address of a pointer?
Should the pointer address be permanent during all exe life?

Thanks,

Tal.
Posted
Updated 10-Mar-12 21:19pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Mar-12 3:09am    
You did not show a declaration of pPointer.
What address do you want to print: of the object pointer by a pointer or a pointer as object?
What's the problem?
What is permanent and what is not depends on your code.
--SA

I think you need a pointer to pointer. read this :

Pointer to pointer

-------------------------------
Regards

H.Maadani
 
Share this answer
 

If you want print the address of the pPointer variable, you can use the & operator to obtain the address of pPointer, like the following:


C++
void *val1 = &pPointer;
 
Share this answer
 
Use should use the p specifier[^].
C++
sprintf_s(DebugString,"pPointer: %p\n",val1);
 
Share this answer
 
say p is a integer pointer,(int *p),
if you want to print the addess of p, you can use printf("%p",&p),
if you want to print the address in p you can use printf("%p",p);

Hopes this helps
http://cgmath.blogspot.com
jkchan
 
Share this answer
 
Hi,

Just change the following line from
sprintf_s(DebugString,"pPointer: %d\n",val1);
to
sprintf_s(DebugString,"pPointer: %u\n",val1);
or
sprintf_s(DebugString,"pPointer: %p\n",val1);


Thanks,
 
Share this answer
 
I think you can change a little . Using %d however works but better is using %p.
Because addresses are of value DWORD (long)in 32 bit machines. So %d wont be a good idea.

Should the pointer address be permanent during all exe life?

It depends on where the pointer is referring.
Pointer addressing variables in stack region are not permanent.
While those addressing in Data may or may not remain constant.
And those pointing to code segment are permanent throughout exe life..

..Hope you got a solution .... :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900