I wanted to read a value which is stored at an address whose absolute value is known. I am wondering how could I achieve this. For example. If a value is stored at 0xff73000. Then is it possible to fetch the value stored here through the C code. Thanks in advance
Asked
Active
Viewed 9.0k times
20
-
3Technically, yes. If the value is an integer, for example, you could declare: `int *xp = 0xff73000;` then in your code reference `*xp`. Used on embedded systems, typically. If you try this on Windows or Linux you'll likely get a memory exception. – lurker Sep 11 '13 at 12:29
-
each program has its own memory allocated by OS therefore it is "impossible" to fetch such things. Read more [here](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366525(v=vs.85).aspx) [windows] – Kyslik Sep 11 '13 at 12:29
-
How do you know that the value is stored at that address? The point of protected mode is that it's impossible for an application to use hardcoded memory addresses. That's probably up in memory-mapped device land. Are you sure you want to do that? – Dan Sep 11 '13 at 12:32
-
1Would you care explaining what's the context of your problem, please? Why do you need to do that? Are you programming for the bare metal? – LorenzoDonati4Ukraine-OnStrike Sep 11 '13 at 12:34
-
First you need to define what address space you mean -- physical address or virtual address? User space or Kernel space? – Chris Dodd Apr 26 '20 at 19:52
3 Answers
25
Two ways:
1. Cast the address literal as a pointer:
char value = *(char*)0xff73000;
Cast the literal as a pointer to the type.
and
De-reference using the prefix *
.
Same technique applies also to other types.
2. Assign the address to a pointer:
char* pointer = (char*)0xff73000;
Then access the value:
char value = *pointer;
char first_byte = pointer[0];
char second_byte = pointer[1];
Where char
is the type your address represents.

Joundill
- 6,828
- 12
- 36
- 50

Zdeněk Gromnica
- 854
- 2
- 14
- 31
19
Just assign the address to a pointer:
char *p = (char *)0xff73000;
And access the value as you wish:
char first_byte = p[0];
char second_byte = p[1];
But note that the behavior is platform dependent. I assume that this is for some kind of low level embedded programming, where platform dependency is not an issue.

Peter Cordes
- 328,167
- 45
- 605
- 847

Juraj Blaho
- 13,301
- 7
- 50
- 96
-
Thanks for the replies, I was trying to access the status register value for an i2c bus driver. Doing the above resulted in a Kernel panic. – niteshnarayanlal Sep 12 '13 at 04:59
-
-
-
wooooooh feel like i just became the master in array after reading your answer : ) – Sanmeet Nov 10 '21 at 05:23
15
char* p = 0x66FC9C;
This would cause this warning :
Test.c: In function 'main': Test.c:57:14: warning: initialization makes pointer from integer without a cast [-Wint-conversion] char* p = 0x66FC9C;
To set a certain address you'd have to do :
char* p = (char *) 0x66FC9C;