我可能对指针的工作方式有根本的误解,但是我认为我可以将变量值分配给指针,但是,每当我打印指针的重新引用值时,它总是0,总是0比"时间戳"的价值。
volatile uint32_t *myAddress = (volatile uint32_t*)0x12341234;
uint32_t timestamp = 0x1111;
*myAddress = timestamp;
无法更新指针值
您的意思是无法更新指向值
做
volatile uint32_t *myAddress = (volatile uint32_t*)0x12341234; uint32_t timestamp = 0x1111; *myAddress = timestamp;
您使用(很可能(无效的地址0x12341234
,以尊重它的行为不确定
做这样的事情:
uint32_t v;
volatile uint32_t *myAddress = &v;
uint32_t timestamp = 0x1111;
*myAddress = timestamp;
// now v values 0x1111
示例:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint32_t v = 0;
volatile uint32_t *myAddress = &v;
uint32_t timestamp = 0x1111;
*myAddress = timestamp; // now v values 0x1111
printf("0x%x 0x%xn", (unsigned) v, (unsigned) *myAddress);
return 0;
}
汇编和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
0x1111 0x1111
pi@raspberrypi:/tmp $
volatile uint32_t *myAddress = (volatile uint32_t*)0x12341234;
这种固定地址指针在UC开发中常用,以访问内存映射的硬件寄存器或确切地址的内存。
例如:
(ARM STM32(
volatile uint32_t *initialSP = (volatile uint32_t *)(0x8000000);
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
GPIOA -> MODER = value;
地址必须具有物理意义 - 即必须有效。
您可能不是,您会得到硬件或内存故障。