为什么解引用运算符(*)也用于声明指针?
我不确定这是否是一个适当的编程问题,但它一直困扰着我,我想知道我是否是唯一的一个。
最初学习 C++ 时,我了解引用的概念,但指针让我感到困惑。你问为什么?因为你声明指针的方式。
请考虑以下情况:
void foo(int* bar)
{
}
int main()
{
int x = 5;
int* y = NULL;
y = &x;
*y = 15;
foo(y);
}
函数 foo(int*)
采用 int
指针作为参数。由于我已将 y
声明为 int
指针,因此我可以将 y
传递给 foo
,但是当第一次学习 C++ 时我将 *
符号与取消引用相关联,因此我认为需要传递一个取消引用的 int
。我会尝试将 *y
传递到 foo
中,这显然不起作用。
使用单独的运算符来声明指针不是更容易吗? (或用于取消引用)。例如:
void test(int@ x)
{
}
I'm not sure if this is a proper programming question, but it's something that has always bothered me, and I wonder if I'm the only one.
When initially learning C++, I understood the concept of references, but pointers had me confused. Why, you ask? Because of how you declare a pointer.
Consider the following:
void foo(int* bar)
{
}
int main()
{
int x = 5;
int* y = NULL;
y = &x;
*y = 15;
foo(y);
}
The function foo(int*)
takes an int
pointer as parameter. Since I've declared y
as int
pointer, I can pass y
to foo
, but when first learning C++ I associated the *
symbol with dereferencing, as such I figured a dereferenced int
needed to be passed. I would try to pass *y
into foo
, which obviously doesn't work.
Wouldn't it have been easier to have a separate operator for declaring a pointer? (or for dereferencing). For example:
void test(int@ x)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在C 语言的发展中,Dennis Ritchie 解释了他的推理因此:
In The Development of the C Language, Dennis Ritchie explains his reasoning thusly:
如果这样写的话,原因就更清楚了:
也就是说,x和*y都是整数。因此 y 是一个 int *。
The reason is clearer if you write it like this:
That is, both x and *y are ints. Thus y is an int *.
这是早于 C++ 的语言决定,因为 C++ 从 C 继承了它。我曾经听说其动机是声明和使用是等效的,即给定声明
int *p;
表达式*p
的类型为int
,与int i;
的表达式i
的类型相同类型int
。That is a language decision that predates C++, as C++ inherited it from C. I once heard that the motivation was that the declaration and the use would be equivalent, that is, given a declaration
int *p;
the expression*p
is of typeint
in the same way that withint i;
the expressioni
is of typeint
.因为委员会以及在 C++ 标准化之前的几十年里开发 C++ 的人决定
*
应保留其原始的三个含义:符 正确地表明
*
(以及类似的&
)的多重含义令人困惑。多年来我一直认为它们是语言新手理解的重大障碍。为什么不为 C++ 选择另一个符号?
向后兼容性是根本原因...最好在新的上下文中重用现有符号,而不是通过将以前非运算符转换为新含义来破坏 C 程序。
为什么不为 C 选择另一个符号呢?
虽然不可能确切知道,但可以并且已经提出了一些论据。最重要的是这样的想法:
这也是为什么 C 程序员倾向于[需要引用]更喜欢将星号向右而不是向左对齐,即:
尽管这两种变体都在两种语言的程序中都存在语言,各不相同。
Because the committee, and those that developed C++ in the decades before its standardisation, decided that
*
should retain its original three meanings:You're right to suggest that the multiple meanings of
*
(and, similarly,&
) are confusing. I've been of the opinion for some years that it they are a significant barrier to understanding for language newcomers.Why not choose another symbol for C++?
Backwards-compatibility is the root cause... best to re-use existing symbols in a new context than to break C programs by translating previously-not-operators into new meanings.
Why not choose another symbol for C?
It's impossible to know for sure, but there are several arguments that can be — and have been — made. Foremost is the idea that:
This is also why C programmers tend to[citation needed] prefer aligning their asterisks to the right rather than to the left, i.e.:
though both varieties are found in programs of both languages, varyingly.
专家 C 编程:深度 C 秘密的第 65 页包括以下内容:然后,还有 C 哲学,即对象的声明应该看起来像它的使用。
C 编程语言,第二版(又名 K&R) 包括:声明符读作一个断言,当它的标识符出现在与声明符相同形式的表达式中时,它会生成指定类型的对象。
我更喜欢 van der Linden 的说法。
Page 65 of Expert C Programming: Deep C Secrets includes the following: And then, there is the C philosophy that the declaration of an object should look like its use.
Page 216 of The C Programming Language, 2nd edition (aka K&R) includes: A declarator is read as an assertion that when its identifier appears in an expression of the same form as the declarator, it yields an object of the specified type.
I prefer the way van der Linden puts it.
哈哈,我感受到你的痛苦,我也遇到了同样的问题。
我认为指针应该声明为
&int
因为指针是某物的地址是有意义的。后来我自己想了一下,C中的每个类型都可以向后读取,比如
int * const x
可以读取为
x const * int
常量
x
,当取消引用时(用*
表示),其类型为int
。因此,必须取消引用的东西必须是指针。
Haha, I feel your pain, I had the exact same problem.
I thought a pointer should be declared as
&int
because it makes sense that a pointer is an address of something.After a while I thought for myself, every type in C can be read backwards, like
int * const x
can be read as
x const * int
A constant
x
, when dereferenced (signaled with*
) is of typeint
.So something that has to be dereferenced, has to be a pointer.