[英]Print and edit pointer by address in c
我想將值打印到指針中並編輯位於 0xabcd 地址的指針值
例如 0xabcd ->0xeeeee,我希望 0xabcd 指向 0xaaaa。
int * buff = 0xabcd;
print("the value is %p",*buff); // here is want to see 0xeeeee
*buff =0xaaaa;
那正確嗎?
不它不是。
int * buff = 0xabcd;
它將 integer 值轉換為指向 int 的指針分配給指針。 地址 0xabcd 指向 int 數組的可能性非常小。 取消引用它會調用未定義的行為。
%p
是打印指針而不是它引用的 integer。 第二個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);
但是這種形式我們經常在低級編程中使用,我們知道硬件寄存器或緩沖區的地址。
例子:
volatile int *ADC_Result = (volatile int *)0x45000000;
然后你可以看到這個寄存器里有什么
printf("ADC value: %d\n", *ADC_Result);
但是您需要使用正確的格式(在這種情況下%d
打印整數)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.