printing address using pointer

N

nilesh

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
 
U

Ulrich Eckhardt

nilesh said:
int i, *ip;
i = 10;
ip = &i;
printf("%x\n", ip);

I'm not sure the %x is apropriate here....
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 ?

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
 
J

Joona I Palaste

I'm not sure the %x is apropriate here....

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

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
B

Barry Schwarz

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

printf("%p\n", (void*)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



<<Remove the del for email>>
 
J

John Bode

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

printf ("%p\n", (void*) 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 ?

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top