简体   繁体   English

为什么指针 (*) 和数组 ([]) 符号绑定到变量名而不是变量声明中的类型?

[英]Why pointer (*) and array ([]) symbols are bound to variable name and not to type in variable declaration?

There're a lot of questions on SO about details of pointer and array declarations in C (and C subset of C++).关于 C(以及 C++ 的 C 子集)中指针和数组声明的细节,SO 上有很多问题。
I'm more interested in why .我对为什么更感兴趣。
Why do we have to put * , [] in front of every variable when we declare several pointers/arrays in a row?当我们连续声明多个指针/数组时,为什么必须将* , []放在每个变量的前面?

int *a, *b;
int c[1], d[1];

Why do we have to type out things after/around variable names in function pointers?为什么我们必须在函数指针中的变量名之后/周围键入内容?

void (*foo_ptr)(int, int);

Why do we have this feature that confuses a lot of newcomers, when even compilers recognize and report these things as part of type?为什么我们有这个让很多新手感到困惑的功能,甚至编译器也会识别并报告这些东西作为类型的一部分? Ex: function foo accepts int** but it was given int*例如: function foo accepts int** but it was given int*

I guess I'm looking for intuition behind it that caused it being created this way, so that I can apply it to my understanding of the language.我想我正在寻找它背后的直觉,导致它以这种方式被创建,以便我可以将它应用于我对语言的理解。 Right now I just don't see it...现在我只是看不到它......

Kernighan and Ritchie write, in The C Programming Language , 1978, page 90: Kernighan 和 Ritchie 在The C Programming Language 1978 年第 90 页中写道:

The declaration of the pointer px is new.指针px的声明是新的。

int *px;

is intended as a mnemonic;旨在作为助记符; it says the combination *px is an int , that is, if px occurs in the context *px , it is equivalent to a variable of the type int .它表示组合*px是一个int ,也就是说,如果px出现在上下文*px ,它相当于一个int类型的变量。 In effect, the syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear.实际上,变量声明的语法模仿了变量可能出现的表达式的语法。 This reasoning is useful in all cases involving complicated declarations.这种推理在所有涉及复杂声明的情况下都很有用。 For example,例如,

double atof(), *dp;

says that in an expression atof() and *dp have values of type double .表示在表达式中atof()*dp具有double类型的值。

Thus, we see that, in declarations such as int X, Y, Z , X , Y , and Z give us “pictures” of expressions, such as b , *b , b[10] , *b[10] , and so on.因此,我们看到,在诸如int X, Y, ZXYZ为我们提供了表达式的“图片”,例如b*bb[10]*b[10]和很快。 The actual type for the declared identifier is derived from the picture: Since *b[10] is an int , then b[10] is a pointer to an int , so b is an array of 10 pointers to int .声明的标识符的实际类型源自图片:由于*b[10]是一个int ,那么b[10]是一个指向int的指针,所以b是一个包含 10 个指向int指针的数组。

Why do we have to put *, [] in front of every variable when we declare several pointers/arrays in a row?当我们连续声明多个指针/数组时,为什么必须在每个变量前面放置 *, [] ?

The answer would be: explicit is better than implicit.答案是:显式优于隐式。 In such a case we can even declare different types on the same row, int *a, *b, c;在这种情况下,我们甚至可以在同一行上声明不同的类型, int *a, *b, c; and so on, in another case it would be too messy.依此类推,在另一种情况下,它会太乱。 The same true for the second question.第二个问题同样如此。

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

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