Re: Memory address and Pointer
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 30 Nov 2007 01:16:33 -0800
erfan <zhaoerfan@xxxxxxxxx> writes:
vc++6.0,winxp.
I use this code to see the memory address and the real value`s
address below:
#include<stdio.h>
int main()
{
int a=22;
int *p;
int i;
p=&a;
printf("%x\n",&a);
printf("%p\n",*p);
}
the result is:
12ff7c
00000016
Press any key to continue
You're using incorrect formats in both your printf calls. "%x" prints
an unsigned int in hexadecimal; you're passing it a pointer to int.
"%p" expects a pointer, specifically a void*; you're passing it an
int.
the pointer p is pointing to a value which is 22,and the address of
the value is 0012ff7c.
Pointers point to objects, not to values; objects have values.
p points to an *object* whose value is 22. The address of that object
happens to be 0012ff7c (when displayed in hexadecimal).
i want to compare the memory address with the value address to see
wheather they are the same.
I don't know what you mean by "memory address" vs. "value address".
So ,next,i use win Debug to help me.
-d 0012:ff7c
but the output is 00 00 00 00 00 00 00 00 00......
what`s wrong? as far as i know,the value of a,must have it`s address
in the memory,and when i search the address,why there is nothing left
in it. there must be some mistake in my understanding,i really expect
your words~~
I've never used win Debug, so I can't help you with that.
Here's a corrected and expanded version of your program. I'm not sure
what you're asking, but perhaps this will help you answer it anyway.
#include <stdio.h>
int main(void)
{
int a = 22;
int *p = &a;
printf("a = %d\n", a);
printf("&a = %p\n", (void*)&a);
printf("p = %p\n", (void*)p);
printf("*p = %d\n", *p);
return 0;
}
--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- Memory address and Pointer
- From: erfan
- Memory address and Pointer
- Prev by Date: Re: main function
- Next by Date: Re: look up tables
- Previous by thread: Re: Memory address and Pointer
- Next by thread: Re: Memory address and Pointer
- Index(es):
Relevant Pages
|
|