erfan wrote:
> vc++6.0,winxp.
To this group it shouldn't matter.
> 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);
The 'x' format specifier expects an unsigned int argument. The
expression '&a' yields a value of type int *. Therefore there is a
mismatch which leads to undefined behaviour. To print a pointer value
use the 'p' format specifier which just for this purpose. It expects a
corresponding argument of type void *, so you need to cast the
appropriate argument.
printf("&a = %p\n", (void *)&a);
> printf("%p\n",*p);
The expression '*p' yields a value of type int. The format specifier 'p'
expects a corresponding argument of type void *, so once again there is
a mismatch. Do:
printf("*p = %d\n", *p);
> }
> the result is:
> 12ff7c
> 00000016
> Press any key to continue
>
> the pointer p is pointing to a value which is 22,and the address of
> the value is 0012ff7c.
'p' holds the address of an int object, in this case 'a', which, in
turn, contains a value that is 22 in base 10.
> i want to compare the memory address with the value address to see
> wheather they are the same.
Which memory address? And the phrase "value address" is totally
meaningless. Only objects have addresses, not values. For example:
int foo = 10;
int bar;
bar = foo + 10;
Now in the last statement both 'foo' and '10' resolve to a value, but
only 'foo' is an object and hence has an address (unless it was
declared as a register object). The expression '10' is simply a source
code literal which has (from C's point of view) no address. Note though
that a string literal _does_ yield an 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~~
Try the suggested changes and see. I think you are confused between
memory addresses and memory content and the involvement of pointers in
both. If you can be more clear with your questions, we can give you
more helpful answers. Also be sure to see the group's de facto FAQ at:
<http://www.c-faq.com/>
|