I have a memory address. I want to output the content of this memory address.how can I do this in C ?
I have a memory address. I want to output the content of this memory address.how can I do this in C ?
Something like this:
Code:volatile unsigned char *addr = (unsigned char *)0x80000000;
Just because I don't care doesn't mean I don't understand.
Thank you.
Could you possible give an explanation as to how this works? not sure.
It's pretty straightforward. You cast the address--as a literal value--to a pointer of suitable type for how you're going to use it. The pointer then points to that address and you can use it just like a pointer to any other memory. The only difference is that you should make the pointer volatile because it refers to memory outside of your control.
Just because I don't care doesn't mean I don't understand.
It doesn't work with 32-bit compilers on XP MS-Windows operating system because that memory address is not mapped into the program's address space. Don't know about *nix.Code:#include <stdio.h> int main() { volatile unsigned char *addr = (unsigned char *)0x80000000; printf("%d\n", *addr); return 0; }
Last edited by Ancient Dragon; 09-11-2005 at 03:08 PM.
Well naturally it's not portable. Presumably bhuwan already knows that it works on his system, or he'll be back shortly to ask why it doesn't work.
Just because I don't care doesn't mean I don't understand.
I shrunk the code down to:
I am on WIN XP and it worked fine. Thanks for your help.Code:int tmpaddr = X value; // input from user char *addr = (char *)tmpaddr; // convert // then i printed the contents using printf
Originally Posted by NarfPs I am not sure how the (char *) worksOriginally Posted by Narf
is that simply saying convert the value to a char type pointer?
Last edited by bhuwan; 09-11-2005 at 03:26 PM. Reason: added code
Originally Posted by bhuwan
I can get lots of crap to compile too, but it doesn't mean it will run. your code is not dereferencing the addr variable. Yes, it will compile, but you can't do anything with it. Try this, which crashes too.
Code:int main() { unsigned int X = 0x80000000; volatile unsigned char *addr = (unsigned char *)X; unsigned n = *addr; printf("%d\n", n); return 0; }
Last edited by Ancient Dragon; 09-11-2005 at 03:46 PM.
> I have a memory address.
How about starting at the beginning, by stating what it is you actually hope to achieve with this.
Because very few machines will tolerate you picking out random memory locations and then going and looking to see what's there.
Don't forget to mention what your real operating system and compiler is.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Sorry for the confusion but i've gotten the task i wanted to do to work properly. All I am asking is this:
Ps I am not sure how the (char *) works
is that simply saying convert the value to a char type pointer?
Thanks for your help, but I should have said it more claerly above: what I wanted to do has worked out fine. But again thanks fory our helpOriginally Posted by Ancient Dragon
its called typcasting -- telling the compiler to redefined an object from one type to another. Otherwise, it does nothing, except sometimes hide errors.Originally Posted by bhuwan
What exactly is the "CHAR *"Originally Posted by Ancient Dragon