Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a value from an absolute address through C code

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

like image 758
niteshnarayanlal Avatar asked Sep 11 '13 12:09

niteshnarayanlal


People also ask

How do I print the value of a pointer?

Printing pointers. You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.

What is &Var in C?

Address in CIf you have a variable var in your program, &var will give you its address in the memory. We have used address numerous times while using the scanf() function. scanf("%d", &var); Here, the value entered by the user is stored in the address of var variable. Let's take a working example.

How do you print the address of a variable in C?

To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator. By using pointer variable.

What does * p mean in C?

In C programming language, *p represents the value stored in a pointer. ++ is increment operator used in prefix and postfix expressions.


1 Answers

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.

like image 97
Juraj Blaho Avatar answered Sep 25 '22 08:09

Juraj Blaho