How do you assign a pointer address manually (e.g. to memory addres开发者_运维百科s 0x28ff44
) in the C programming language?
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.
Your code would be like this:
int *p = (int *)0x28ff44;
int
needs to be the type of the object that you are referencing or it can be void
.
But be careful so that you don't try to access something that doesn't belong to your program.
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
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)
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
精彩评论