简体   繁体   English

按 c 中的地址打印和编辑指针

[英]Print and edit pointer by address in c

I want to print the value into pointer and edit the pointer's value that located at 0xabcd address我想将值打印到指针中并编辑位于 0xabcd 地址的指针值

For example 0xabcd ->0xeeeee, I want that 0xabcd will point to 0xaaaa.例如 0xabcd ->0xeeeee,我希望 0xabcd 指向 0xaaaa。

int * buff = 0xabcd;
print("the value is %p",*buff); // here is want to see 0xeeeee 
*buff =0xaaaa;

is that right?那正确吗?

No, it is not.不它不是。

int * buff = 0xabcd;

It assigns the pointer with the integer value converted to pointer to int.它将 integer 值转换为指向 int 的指针分配给指针。 It is very very unlikely the address 0xabcd to point to the int array.地址 0xabcd 指向 int 数组的可能性非常小。 Dereferencing it invokes Undefined Behaviour.取消引用它会调用未定义的行为。

The %p is to print pointer not the integer referenced by it. %p是打印指针而不是它引用的 integer。 Second UB第二个UB

int a,b;
int * buff = &a;
print("the value is %p",(void *)buff); 
*buff =0xaaaa;
print("the value is %d",*buff); 
buff = &b;
print("the value is %p",(void *)buff); 

But this form us used very often in the low level programming where we know the addresses of the hardware registers or buffers.但是这种形式我们经常在低级编程中使用,我们知道硬件寄存器或缓冲区的地址。

Example:例子:

volatile int *ADC_Result = (volatile int *)0x45000000;

Then you can see what is in this register然后你可以看到这个寄存器里有什么

printf("ADC value: %d\n", *ADC_Result);

But you need to use the correct format (in this case %d to print the integer)但是您需要使用正确的格式(在这种情况下%d打印整数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM