[英]How to assign pointer address manually in C programming language?
如何在 C 编程语言中手动分配指针地址(例如分配给内存地址0x28ff44
)?
Like this:像这样:
void * p = (void *)0x28ff44;
Or if you want it as a char *
:或者,如果您希望将其作为char *
:
char * p = (char *)0x28ff44;
...etc. ...等。
If you're pointing to something you really, really aren't meant to change, add a const
:如果你指的是你真的,真的不想改变的东西,添加一个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
. int
需要是您引用的对象的类型,或者它可以是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基本上在嵌入式平台中,我们直接使用地址而不是名称
Writing: 写作:
int *p=(int *)0x1234 = 10;
is not legal for some compilers. 某些编译器不合法。 You need to split in two instructions: 您需要分成两个指令:
int *p=(int *)0x1234; *p = 10;
let's say you want a pointer to point at the address 0x28ff4402, the usual way is假设你想要一个指向地址 0x28ff4402 的指针,通常的方法是
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.