简体   繁体   English

volatile unsigned int * const

[英]volatile unsigned int * const

In the following line of code, what is the purpose of the const keywords? 在以下代码行中, const关键字的目的是什么?

volatile unsigned int * const UART0DR = (unsigned int *)0x10009000;

I understand the volatile unsigned int * bit, but why is const there? 我理解volatile unsigned int * bit,但为什么const存在?

const and volatile are called "type qualifiers". constvolatile被称为“类型限定符”。 Their syntax is one of the most confusing things in C. 他们的语法是C中最令人困惑的事情之一。

First of all we have the case with an ordinary variable. 首先,我们有一个普通变量的情况。 You usually write the type qualifier before the variable name: const int x; 您通常在变量名之前编写类型限定符: const int x; , but is also fine to write it after the variable name: int const x; ,但在变量名后写它也很好: int const x; . The meaning is the same, the latter syntax is just there to confuse. 意思是一样的,后面的语法就是混淆了。

When declaring a pointer, you are concerned with two types: the type of the pointer itself, and the type pointed at. 在声明指针时,您会关注两种类型:指针本身的类型和指向的类型。 With ordinary pointers, this is trivial: int* x means that the pointer type is pointer-to-int, and the pointed-to-type is int. 对于普通指针,这是微不足道的: int* x表示指针类型是指向int的指针,而指向的类型是int。

If the pointed-to-type is const int, the pointer is declared as const int* x . 如果指向的类型是const int,则指针被声明为const int* x The pointer is of type pointer-to-const-int, and the pointed-to type is const int. 指针的类型为pointer-to-const-int,指向的类型为const int。 If you want to confuse everyone, you can also write int const* x , it is equivalent. 如果你想混淆每个人,你也可以写int const* x ,它是等价的。

Let us say that we want the pointer itself to be constant as well. 让我们说我们希望指针本身也是常量。 For some reason we want to block the programmer from changing the address pointed at. 出于某种原因,我们希望阻止程序员更改指向的地址。 For example, perhaps the pointer is to be stored in read-only memory of an embedded system. 例如,指针可能存储在嵌入式系统的只读存储器中。 A constant pointer to data, is declared as int*const x; 一个指向数据的常量指针,声明为int*const x; with the const keyword after the *. 在*之后使用const关键字。 The pointed-to-type in this case is int , and not const int . 这种情况下指向的类型是int ,而不是 const int (I try to write the const together with the *, with no space in between, to give emphasis that the pointer is constant) (我尝试将const与*一起编写,中间没有空格,强调指针是常量)

If we want the pointed-to-type to be constant as well, we would have to combine the two above declarations: const int*const x; 如果我们希望指向类型也是常量,我们必须结合上面两个声明: const int*const x; means a constant pointer pointing at a constant int. 表示指向常量int的常量指针。

const int x;          // x is a constant data variable
int const x;          // x is a constant data variable

const int* x;         // x is a non-constant pointer to constant data
int const* x;         // x is a non-constant pointer to constant data 

int*const x;          // x is a constant pointer to non-constant data

const int*const x;    // x is a constant pointer to constant data

In the above examples I have used the type qualifier const . 在上面的例子中,我使用了类型限定符const But volatile is also a type qualifier, and it behaves in exactly the same manner! volatile也是一个类型限定符,它的行为方式完全相同! It is possible to declare non-volatile pointers to volatile data etc. 可以向易失性数据等声明非易失性指针。

And finally, it is possible to combine different type qualifiers. 最后,可以组合不同的类型限定符。 For example a volatile const int* is a non-constant, non-volatile pointer to volatile const data. 例如, volatile const int*是指向易失性const数据的非常量非易失性指针。 Once again we have the wonderful option to mix the order of these to confuse, so we can also write volatile int const * or int volatile const * etc and it all means the same. 再一次,我们有一个很好的选择,混合这些的顺序混淆,所以我们也可以写volatile int const *int volatile const *等,这一切都意味着相同。

I understand the volatile unsigned int * bit, but why is const there? 我理解volatile unsigned int * bit,但为什么const存在?

It means that the pointer is constant, which means that the pointer cannot(rather should not) point to any other address. 这意味着指针是常量,这意味着指针不能(而不应该)指向任何其他地址。

Note the following, constructs: 请注意以下构造:

volatile unsigned int * const UART0DR = 

Pointer is constant here. 指针在这里是不变的。


volatile unsigned int const * UART0DR = 

The pointed address is constant here. 指向的地址在这里是不变的。


volatile unsigned int const * const UART0DR = 

The pointed address as well as the pointer is constant in this case. 在这种情况下,指向的地址以及指针是常量。

From http://cdecl.org 来自http://cdecl.org

volatile unsigned int *const a
=>
declare a as const pointer to volatile unsigned int

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

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