它不是指向指针的指针。想一想:假设我们有一个指针:
uint8_t * cptr;
这可以是全局变量,也可以只是函数中的参数。
你怎么看它指出的价值?
c = * cptr;
如何将值写入其指向的位置?
* cptr = c;
所以这是第一个'*'
接下来是真正的“指针”演员(volatile uint8_t *)
需要强制转换,因为文字0x5260只是被编译器视为一个int,而不是指向SOMETHING的指针(SOMETHING非常重要,在我们的例子中是'uint8_t')。想一想:如果5260是指向uint8_t(一个字节)的指针,您将读/写一个字节。如果它是一个指向int的指针,你将读/写两个字节!等等。这就是为什么默认情况下不能将简单的int视为指针。指针是什么?
关于'volatile':这只是告诉编译器生成代码,每隔一段时间就读取(或写入)地址。因为今天的编译器非常智能,通常以非常意想不到的方式优化内存读/写。
以上来自于谷歌翻译
以下为原文
It's not pointer to a pointer. Think of that: suppose we have a pointer:
uint8_t *cptr;
This can be a global variable, or may be just a parameter in a function.
How do you read the value pointed by it?
c = *cptr;
How do you write a value to the location pointed by it?
*cptr = c;
So here is the first '*'
Followed by the real 'pointer' cast (volatile uint8_t *)
The cast is needed because the literal 0x5260 is just treated as an int by the compiler, not as a pointer to SOMETHING (SOMETHING being very important, in our case 'uint8_t'). Think of that: if 5260 is a pointer to an uint8_t (a byte), you will read/write a byte. If it is a pointer to int, you will read/write two bytes ! And so on. That's why a simple int cannot be treated as a pointer by default. A pointer to what?
About the 'volatile': this is just to tell the compiler to generate code that reads (or writes) to the address EVERY TIME you say so. Because today compilers are very smart and usually optimize the memory read/writes in very unexpected ways.
它不是指向指针的指针。想一想:假设我们有一个指针:
uint8_t * cptr;
这可以是全局变量,也可以只是函数中的参数。
你怎么看它指出的价值?
c = * cptr;
如何将值写入其指向的位置?
* cptr = c;
所以这是第一个'*'
接下来是真正的“指针”演员(volatile uint8_t *)
需要强制转换,因为文字0x5260只是被编译器视为一个int,而不是指向SOMETHING的指针(SOMETHING非常重要,在我们的例子中是'uint8_t')。想一想:如果5260是指向uint8_t(一个字节)的指针,您将读/写一个字节。如果它是一个指向int的指针,你将读/写两个字节!等等。这就是为什么默认情况下不能将简单的int视为指针。指针是什么?
关于'volatile':这只是告诉编译器生成代码,每隔一段时间就读取(或写入)地址。因为今天的编译器非常智能,通常以非常意想不到的方式优化内存读/写。
以上来自于谷歌翻译
以下为原文
It's not pointer to a pointer. Think of that: suppose we have a pointer:
uint8_t *cptr;
This can be a global variable, or may be just a parameter in a function.
How do you read the value pointed by it?
c = *cptr;
How do you write a value to the location pointed by it?
*cptr = c;
So here is the first '*'
Followed by the real 'pointer' cast (volatile uint8_t *)
The cast is needed because the literal 0x5260 is just treated as an int by the compiler, not as a pointer to SOMETHING (SOMETHING being very important, in our case 'uint8_t'). Think of that: if 5260 is a pointer to an uint8_t (a byte), you will read/write a byte. If it is a pointer to int, you will read/write two bytes ! And so on. That's why a simple int cannot be treated as a pointer by default. A pointer to what?
About the 'volatile': this is just to tell the compiler to generate code that reads (or writes) to the address EVERY TIME you say so. Because today compilers are very smart and usually optimize the memory read/writes in very unexpected ways.
举报