C 中这些声明有什么区别?

发布于 2024-07-08 00:32:48 字数 233 浏览 10 评论 0原文

在 C 和 C++ 中,以下声明有何作用?

const int * i;
int * const i;
const volatile int ip;
const int *i;

上述声明是否有错误?

如果不是的话它们之间的含义和区别是什么?

上述声明有哪些有用的用途(我的意思是在什么情况下我们必须在 C/C++/嵌入式 C 中使用它们)?

In C and C++ what do the following declarations do?

const int * i;
int * const i;
const volatile int ip;
const int *i;

Are any of the above declarations wrong?

If not what is the meaning and differences between them?

What are the useful uses of above declarations (I mean in which situation we have to use them in C/C++/embedded C)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2024-07-15 00:32:48

const int * i;

i 是指向常量整数的指针。 i 可以更改为指向不同的值,但 i 所指向的值无法更改。

int * const i;

i 是指向非常量整数的常量指针。 i 指向的值可以更改,但 i 不能更改为指向不同的值。

const volatile int ip;

这个有点棘手。 ipconst 的事实意味着编译器不会让您更改 ip 的值。 然而,理论上它仍然可以被修改,例如通过获取它的地址并使用const_cast运算符。 这是非常危险的,也不是一个好主意,但这是允许的。 易失性限定符指示任何时候ip被访问时,它应该总是从内存中重新加载,即它不应该被缓存在寄存器中。 这会阻止编译器进行某些优化。 当您有一个可能被另一个线程修改的变量,或者您正在使用内存映射 I/O,或者其他可能导致编译器行为的类似情况时,您需要使用 volatile 限定符可能不会期待。 在同一个变量上使用 constvolatile 是相当不寻常的(但合法)——您通常会看到一个而不是另一个。

const int *i;

这与第一个声明相同。

const int * i;

i is a pointer to constant integer. i can be changed to point to a different value, but the value being pointed to by i can not be changed.

int * const i;

i is a constant pointer to a non-constant integer. The value pointed to by i can be changed, but i cannot be changed to point to a different value.

const volatile int ip;

This one is kind of tricky. The fact that ip is const means that the compiler will not let you change the value of ip. However, it could still be modified in theory, e.g. by taking its address and using the const_cast operator. This is very dangerous and not a good idea, but it is allowed. The volatile qualifier indicates that any time ip is accessed, it should always be reloaded from memory, i.e. it should NOT be cached in a register. This prevents the compiler from making certain optimizations. You want to use the volatile qualifier when you have a variable which might be modified by another thread, or if you're using memory-mapped I/O, or other similar situations which could cause behavior the compiler might not be expecting. Using const and volatile on the same variable is rather unusual (but legal) -- you'll usually see one but not the other.

const int *i;

This is the same as the first declaration.

日久见人心 2024-07-15 00:32:48

可以这么说,您从右到左阅读 C/C++ 中的变量声明。

const int *i;  // pointer to a constant int (the integer value doesn't change)

int *const i;  // constant pointer to an int (what i points to doesn't change)

const volatile int ip;  // a constant integer whose value will never be cached by the system

他们每个人都有自己的目的。 任何 C++ 教科书或半像样的资源都会对每个内容都有解释。

You read variables declarations in C/C++ right-to-left, so to speak.

const int *i;  // pointer to a constant int (the integer value doesn't change)

int *const i;  // constant pointer to an int (what i points to doesn't change)

const volatile int ip;  // a constant integer whose value will never be cached by the system

They each have their own purposes. Any C++ textbook or half decent resource will have explanations of each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文