Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a data in particular address in C [duplicate]

Tags:

c++

c

I need to write 0x00001234 in the address 0x8000000, is it possible in C?

like image 718
vkulkarni Avatar asked Oct 02 '13 09:10

vkulkarni


People also ask

Can two variables have the same address in C?

So not to happen all these, two variables can't have same addresses not in c, anywhere, you can transfer a variable address to store in a variable, later you will learn more about this topic in pointers in c language, Hope you understood!!

How to get an address of a variable in C?

To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.

Does memcpy copy the address?

The memcpy function is used to copy a block of data from a source address to a destination address.

How do you print an address in C?

To print the memory address, we use '%p' format specifier 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.


2 Answers

If you work with hardware register in embedded system then standard way is:

int volatile * const p_reg = (int *) 0x8000000;
*p_reg = 0x1234;

You can have a lot of problems with optimizing compiler, if you omit volatile

like image 143
SergV Avatar answered Oct 13 '22 00:10

SergV


You can, but you will have a segfault 99.9999..9% of the time because your program won't have access on this memory address.

int *nb = (int *) 0x8000000;
*nb = 0x00001234;
like image 26
Thomas Ruiz Avatar answered Oct 13 '22 02:10

Thomas Ruiz