printing address using pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nilesh

    #1

    printing address using pointer

    Hello,

    I have a basic doubt. Consider following program :

    #include<stdio. h>
    int main()
    {
    int i, *ip;
    i = 10;
    ip = &i;
    printf("%x\n", ip);
    return 0;
    }

    When I print the value of ip i.e. the address of integer i, does it
    prints the virtual address of integer i or physical address of i ?

    TIA
    ../nilesh
  • Ulrich Eckhardt

    #2
    Re: printing address using pointer

    nilesh wrote:[color=blue]
    > int i, *ip;
    > i = 10;
    > ip = &i;
    > printf("%x\n", ip);[/color]

    I'm not sure the %x is apropriate here....
    [color=blue]
    > When I print the value of ip i.e. the address of integer i, does it
    > prints the virtual address of integer i or physical address of i ?[/color]

    Since C has no concept of virtual vs physical addresses, it can only print
    the address it has a concept of, and that is pretty much only 'a location
    in memory'.
    I would expect it to print the virtual address though(assuming you have
    such a memory model on your machine), since virtual addresses are the
    thing every program (except the kernel perhaps) should deal with.

    Uli


    Comment

    • Joona I Palaste

      #3
      Re: printing address using pointer

      Ulrich Eckhardt <doomster@knuut .de> scribbled the following:[color=blue]
      > nilesh wrote:[color=green]
      >> int i, *ip;
      >> i = 10;
      >> ip = &i;
      >> printf("%x\n", ip);[/color][/color]
      [color=blue]
      > I'm not sure the %x is apropriate here....[/color]

      It's not. %p is, and ip should be cast to void *.

      --
      /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
      \-------------------------------------------------------- rules! --------/
      "I am not very happy acting pleased whenever prominent scientists overmagnify
      intellectual enlightenment."
      - Anon

      Comment

      • Barry Schwarz

        #4
        Re: printing address using pointer

        On 31 Oct 2004 21:36:54 -0800, nileshks78@yaho o.com (nilesh) wrote:
        [color=blue]
        >Hello,
        >
        >I have a basic doubt. Consider following program :
        >
        >#include<stdio .h>
        >int main()
        >{
        > int i, *ip;
        > i = 10;
        > ip = &i;
        > printf("%x\n", ip);[/color]

        printf("%p\n", (void*)ip);
        [color=blue]
        > return 0;
        >}
        >
        >When I print the value of ip i.e. the address of integer i, does it
        >prints the virtual address of integer i or physical address of i ?
        >
        >TIA
        >./nilesh[/color]



        <<Remove the del for email>>

        Comment

        • John Bode

          #5
          Re: printing address using pointer

          nileshks78@yaho o.com (nilesh) wrote in message news:<29233a5.0 410312136.6e6e9 f8@posting.goog le.com>...[color=blue]
          > Hello,
          >
          > I have a basic doubt. Consider following program :
          >
          > #include<stdio. h>
          > int main()
          > {
          > int i, *ip;
          > i = 10;
          > ip = &i;
          > printf("%x\n", ip);[/color]

          printf ("%p\n", (void*) ip);
          [color=blue]
          > return 0;
          > }
          >
          > When I print the value of ip i.e. the address of integer i, does it
          > prints the virtual address of integer i or physical address of i ?
          >[/color]

          I'm pretty sure that's specific to the implementation; for example, if
          you're running a user program on a *nix or Windows platform, it's
          almost certainly a virtual address. If you were running on bare
          metal, it would probably be a physical address (then again, if you
          were running on bare metal, you probably wouldn't have a working
          printf() available anyway).

          Comment

          Working...