Re: Memory address and Pointer



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"
.



Relevant Pages

  • Re: object creation and naming at runtime?
    ... >>replaced with memory references by the compiler. ... I don't know where the computer will allocate space in ... memory for my int so I'll use a name for the address and let ... In the source code the pointer object (which will ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 2-dimensional arrays and functions
    ... > int* foo ... >> that return memory addresses. ... >> You obviously don't know the difference between a pointer and a memory ... > address of the dynamically allocated memory is stored. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: FIFO Problem
    ... text but a *pointer* to some text. ... pointing to the string in the clients memory. ... if you want to pass a string from one process to the other then ... int length; ...
    (comp.os.linux.development.apps)
  • Re: 2D array of structures
    ... Don't cast the return value of malloc(), ... you allocate here memory for 7 such structures. ... a pointer to the start of this memory, which is of type 'STRUCTURE *' ...
    (comp.lang.c)
  • Re: contiguity of arrays
    ... > consecutive integers and know the address of the first integer, ... No matter how I come into ownership of this memory. ... choose to represent a pointer as an address and a length; ... > type int. ...
    (comp.lang.c)