6

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

unwind
  • 391,730
  • 64
  • 469
  • 606
vkulkarni
  • 849
  • 1
  • 8
  • 12
  • 1
    That address probably won't be valid for writing to. You may be able to force it to exist, for instance with [`mmap` and `MAP_FIXED`](http://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html) if you are using a POSIX OS. – BoBTFish Oct 02 '13 at 09:37
  • 6
    @BoBTFish Maybe he is programming for a microcontroller. – RedX Oct 02 '13 at 09:58
  • What do you need this for? And why? And with what? – zoska Oct 02 '13 at 10:13

3 Answers3

18

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

SergV
  • 1,269
  • 8
  • 20
8

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;
Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33
  • It depends also on your linker script (you can omit some RAM banks and use them manually), though it is not recommended. – lucasg Oct 02 '13 at 09:41
0

Well if this is a beginner assignment in a c class i suspect it is turbo or borland c where you are programming in 16 bit environment with segment offset address scheme. In that case using an int * far ptr, with far being the pointer type to access address out of your current segment would be used. 0xb8000000 used to be the starting address of text mode video memory.

i.e.

int far * p = 0xB8000000;
*p = 'A'; // This would actually print char 'A' on screen
*(p+1) = <some number>; // this would determine the color of char A

Note that this used to be 16 bit programming. So a normal int * would be 16 bit and hence cannot access beyond current segment of memory.

We used to implement printf of our own by directly writing to video mem. This was a class assignment in c programming course over a decade ago. May be it matches your scenario.

This explanation might be helpful as well

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
  • This is not a begineer assignment ..:) – vkulkarni Oct 02 '13 at 10:55
  • Agreed, not a very 'beginner one' given in the start of a programming course. But it certainly does not hold a real world need, even at that time. However, it helps to understand memory, pointers and being able to write functions ourselves. Hence it is more of an academic use. – fkl Oct 02 '13 at 10:57