Memory address and Pointer

Discussion in 'C Programming' started by erfan, Nov 30, 2007.

  1. erfan

    erfan Guest

    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

    the pointer p is pointing to a value which is 22,and the address of
    the value is 0012ff7c.
    i want to compare the memory address with the value address to see
    wheather they are the same.
    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~~
     
    erfan, Nov 30, 2007
    #1
    1. Advertising

  2. erfan wrote:
    > vc++6.0,winxp.
    > I use this code to see the memory address and the real value`s
    > address below:


    Those terms have no meaning and your code is severely broken. Notice
    the changes below:

    #include<stdio.h>

    int main(void)
    {
    int a = 22;
    int *p;
    p = &a;
    #if 0
    /* mha: the line below is an error. &a has type 'int *', but "%x"
    expects the very different type 'unsigned int' */
    printf("%x\n", &a);
    #endif
    printf("%p\n", (void *) &a); /* mha: replacement for the above
    erroneous statement */
    #if 0
    /* mha: the line below is an error. *p has type 'int', but "%p"
    expects the very different type 'void *' */
    printf("%p\n", *p);
    #endif
    printf("%p\n", (void *) p); /* mha: replacement for the above
    erroneous statement */
    return 0;
    }

    [OP's code snipped, being essentially included above]



    >
    > the pointer p is pointing to a value which is 22,and the address of
    > the value is 0012ff7c.
    > i want to compare the memory address with the value address to see
    > wheather they are the same.


    Those terms still mean nothing.
     
    Martin Ambuhl, Nov 30, 2007
    #2
    1. Advertising

  3. erfan

    santosh Guest

    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/>
     
    santosh, Nov 30, 2007
    #3
  4. erfan <> 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) <>
    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"
     
    Keith Thompson, Nov 30, 2007
    #4
  5. erfan wrote:
    > 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


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


    ignoring the errors in printf which others have commented on, the above
    isn't likely to work anyway. In most modern OSes, programmes have their
    own memory space and another application normally can't invade that.
    Also the 'debug' app you're using seems to be a 16-bit app, and the
    address format you're supplying looks very much like a 16-bit format.
    Neither of these will be valid ways of accessing data in a 32-bit
    programme in different memory space!
     
    Mark McIntyre, Nov 30, 2007
    #5
  6. erfan

    erfan Guest

    On 11ÔÂ30ÈÕ, ÏÂÎç11ʱ36·Ö, Mark McIntyre <> wrote:
    > erfan wrote:
    > > 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
    > > 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......

    >
    > ignoring the errors in printf which others have commented on, the above
    > isn't likely to work anyway. In most modern OSes, programmes have their
    > own memory space and another application normally can't invade that.
    > Also the 'debug' app you're using seems to be a 16-bit app, and the
    > address format you're supplying looks very much like a 16-bit format.
    > Neither of these will be valid ways of accessing data in a 32-bit
    > programme in different memory space!- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -


    ---------------------------
    yeah,thank you all for helping.i am new here and happy to see that all
    of you are warm hearted.
    However,when i write a code ,and during the running of the code,as we
    all know,the date is in memory to be deal with. i used pointer in the
    code in order to gain the address to be checked. So may i see the
    real value in the memory during that short time? if yes,how?
    i am reading the Princisple of PC,so i just want to make it clear
    when my code run,how does the registers get to work,and how does the
    assmbeling tools work,such as the data goes from one register to
    another. expecting your reply
     
    erfan, Dec 1, 2007
    #6
  7. erfan

    santosh Guest

    erfan wrote:

    <snip>

    >> ??????? -
    >>
    >> - ??????? -

    >
    > ---------------------------


    *Please* snip unneccessary material like the signature above.

    > yeah,thank you all for helping.i am new here and happy to see that all
    > of you are warm hearted.
    > However,when i write a code ,and during the running of the code,as we
    > all know,the date is in memory to be deal with. i used pointer in the
    > code in order to gain the address to be checked. So may i see the
    > real value in the memory during that short time? if yes,how?
    > i am reading the Princisple of PC,so i just want to make it clear
    > when my code run,how does the registers get to work,and how does the
    > assmbeling tools work,such as the data goes from one register to
    > another. expecting your reply


    My recommendation would be to select a good book on PC architecture and
    x86 assembler programming and read it. They tend to go into such
    details which Standard C is not concerned about and hence this is not
    the right group to ask in.

    Randall Hyde's Art of Assembler and his Write Great Code books may be
    helpful. The former is freely available online and uses his HLA
    language.

    <http://webster.cs.ucr.edu/>
    <http://webster.cs.ucr.edu/WriteGreatCode/index.html>
     
    santosh, Dec 1, 2007
    #7
  8. On Thu, 29 Nov 2007 21:55:33 -0800 (PST), erfan <>
    wrote:

    >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);


    This invokes undefined behavior. %x expects an unsigned int. If you
    want to print an address, use %p and cast the address to void*.

    > printf("%p\n",*p);


    And again. If you want to print the hex representation of an int, use
    %x and cast the value to unsigned.

    >}
    >the result is:
    >12ff7c
    >00000016
    >Press any key to continue


    Where did this line come from? It is not in your code.

    >
    >the pointer p is pointing to a value which is 22,and the address of
    >the value is 0012ff7c.
    >i want to compare the memory address with the value address to see
    >wheather they are the same.


    Then use
    printf("&a=%p, p=%p\n", (void*)&a, (void*)p);

    >So ,next,i use win Debug to help me.


    Questions about specific tools need to be asked in newsgroups that
    deal with those tools.

    >-d 0012:ff7c


    This does not look like the same address. Where did the colon come
    from?

    >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~~
    >



    Remove del for email
     
    Barry Schwarz, Dec 2, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim
    Replies:
    16
    Views:
    852
    Jordan Abel
    Mar 28, 2006
  2. Bryan Parkoff
    Replies:
    2
    Views:
    909
    Gabriel
    Sep 1, 2005
  3. Scott
    Replies:
    6
    Views:
    390
    Luke Meyers
    Mar 27, 2006
  4. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,169
    Keith Thompson
    Sep 20, 2011
  5. junyangzou
    Replies:
    8
    Views:
    188
    junyangzou
    Aug 30, 2013
Loading...

Share This Page