gpt4 book ai didi

c - `const`的特殊用法

转载 作者:行者123 更新时间:2023-11-30 18:47:46 25 4
gpt4 key购买 nike

我已经了解了(如果我错了,请纠正我)很多关于关键字 const 的知识。我了解到,如果你像这样从右向左阅读,那就很容易了:

int a = 0;                 // `a` is an integer. Its value is initialized with zero
const int b = 0; // `b` is an integer which is constant. Its value is set to zero
int * c = &a; // `c` is a pointer to an int. `c` is pointing to `a`.
int * const d= &a; // `d` is a constant pointer to an int. It is pointing to `a`.
const int * e = &a; // `e` is a pointer to an int which is constant. It is pointing to `a`.
int * const f = &a; // `f` is a constant pointer to an int. It is pointing to `a`.
const int * const g = &b; // `g` is a constant pointer to a constant integer. It is pointing to `b`.

但是这是怎么回事:

int const * h;

h指向常量 int 的指针?如果是,与e有什么不同? ?

这是什么:

int const i;

i这与 b 类型相同?

最佳答案

const 限定符可以自由放置在类型关键字之前或之后,只要它们之间没有 * 符号即可。

因此以下内容是等效的:

int const *h;
const int *h;

这些是:

int const i;
const int i;

但这些不是:

int * const p;
const int *p;

关于c - `const`的特殊用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47532744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com