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
3 Answers
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.
Comments
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.
4 Comments
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;
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.