How do you assign a pointer address manually (e.g. to memory address 0x28ff44
) in the C programming language?
Asked
Active
Viewed 1.5e+01k times
66

Peter Brittain
- 13,489
- 3
- 41
- 57

Irakli
- 901
- 1
- 7
- 12
-
7Needless to say, that's a pretty pointless -- if not plain suicidal -- exercise except perhaps in certain embedded systems. – Dec 25 '10 at 22:58
-
3You must Love Segmentation fault then. – Indra Aug 26 '14 at 12:09
-
@IndrajithIndraprastham yeah, that was long ago and I was curious about this stuff – Irakli Aug 26 '14 at 14:35
-
@Irakli: Yes I know it was 3 years back. Today I tried it and got segmentation fault. I tried to change the value of an integer allocated in one program by using another. – Indra Aug 26 '14 at 17:50
5 Answers
92
Like this:
void * p = (void *)0x28ff44;
Or if you want it as a char *
:
char * p = (char *)0x28ff44;
...etc.
If you're pointing to something you really, really aren't meant to change, add a const
:
const void * p = (const void *)0x28ff44;
const char * p = (const char *)0x28ff44;
...since I figure this must be some kind of "well-known address" and those are typically (though by no means always) read-only.

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875
-
2Please correct me if I'm wrong. But isn't it in C undefined behavior to assign an not valid address to a pointer? Same as it is not allowed to produce with pointer arithmetic a result thats not pointing in an valid object, nor inside an array or 1 element behind? – dhein Sep 24 '13 at 11:49
-
3@Zaibis: I don't think C, the language, says anything about it one way or another. The system you're using the language on is what matters. Delnan said it well in the comment on the question: *"Needless to say, that's a pretty pointless -- if not plain suicidal -- exercise except perhaps in certain embedded systems."* There are **very** few use-cases for hardcoded pointer values, but there are some. Back in the day, I seem to remember directly accessing the display memory in DOS... ;-) Hence the "well-known address" comment above. – T.J. Crowder Sep 24 '13 at 12:02
-
@T.J.Crowder: +1 for your concise answer. This involves a C-style cast though, is there a 'modern C++' equivalent that avoids the oldskool cast? I'm thinking along the lines of `xxx_cast
(0xdeafbeef)`. – pauluss86 Jan 30 '14 at 17:37 -
1@pauluss86: There probably is, but I haven't done any C++ in 20 years, and I don't think it had them back then... :-) – T.J. Crowder Jan 30 '14 at 17:42
-
3@T.J.Crowder - Actually, there are *many* uses for hard-coded pointers. This happens all the time in microcontrollers, as well as in bootloaders & drivers of "normal" systems. Usually, the pointer itself could be `const` but the data it points to is not. Most programmers just never see that code or have any idea how it works. Although I might add - this is, increasingly, becoming the place where C is still most relevant as less & less application software has any real reason to be written in C. – Brian McFarland Sep 17 '15 at 14:05
-
@BrianMcFarland: Yup, those are exactly the kinds of applications I was referring to at the end of the answer. – T.J. Crowder Sep 17 '15 at 14:15
-
@T.J.Crowder Yep, the DOS video buffer was at address A000:0000. This is also an example of an address that is not a single number. – dbush Dec 07 '16 at 21:42
-
@dbush: Ah, yes, I remember the halcyon days of the late 80s well. *(Actually, to be honest, I only remember them dimly. :-) )* – T.J. Crowder Dec 07 '16 at 22:23
-
@T.J.Crowder ... I tried this way but this leads to the segmentation fault for *p = 1; – aTechieSmile May 01 '22 at 06:16
-
1@geeeeekyDeveloper - That means you're setting `p` to point to memory you're not allowed to write to. – T.J. Crowder May 01 '22 at 07:45
-
-
Is it possible to have a pointer `p` point to a specific memory address and then assign to that address using `*p`? – Franky Apr 12 '23 at 09:47
-
@Franky - I don't know, I haven't used C++ in nearly 30 years and it's changed a lot in that tijme. I strongly suspect that you can, yes, but that's as sure as I can be. – T.J. Crowder Apr 12 '23 at 09:49
5
int *p=(int *)0x1234 = 10; //0x1234 is the memory address and value 10 is assigned in that address
unsigned int *ptr=(unsigned int *)0x903jf = 20;//0x903j is memory address and value 20 is assigned
Basically in Embedded platform we are using directly addresses instead of names
-
1Writing: int *p=(int *)0x1234 = 10; is not legal for some compilers. You need to split in two instructions: int *p=(int *)0x1234; *p = 10; – Phil Nov 06 '19 at 22:36
-
Not to mention that you can't assign values to random memory addresses that your program doesn't "own". If you try to access the value of a memory address (read or write), the program will likely close. The OS prohibits you. This is so that the OS blocks malicious programs from changing the memory which could lead to harmful faults. – Themud Feb 21 '20 at 12:17
-
@Themud, you are right, you may get `Segmentation fault (core dumped)` just by trying to read other program's memory. – Smeterlink Apr 29 '20 at 18:06
1
let's say you want a pointer to point at the address 0x28ff4402, the usual way is
uint32_t *ptr;
ptr = (uint32_t*) 0x28ff4402 //type-casting the address value to uint32_t pointer
*ptr |= (1<<13) | (1<<10); //access the address how ever you want
So the short way is to use a MACRO,
#define ptr *(uint32_t *) (0x28ff4402)

Yasir
- 73
- 1
- 6
0
In embedded systems while assigning a memory
volatile
must be used, else it is likely that compiler optimization ignores the updated values and uses the old values. Below is a safe method to access the memory
#define ptrVar (*((volatile unsigned long *)0x400073FC)) //ptrVar points to memory location 0x400073FC
ptrVar =10// write 10 at the memory location

Pawan Kumar
- 114
- 9