本文介绍了为什么星号在变量名之前,而不是类型之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么大多数 C 程序员都这样命名变量:
Why do most C programmers name variables like this:
int *myVariable;
而不是这样:
int* myVariable;
两者都有效.在我看来,星号是类型的一部分,而不是变量名称的一部分.谁能解释一下这个逻辑?
Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?
推荐答案
它们完全等效.然而,在
They are EXACTLY equivalent.However, in
int *myVariable, myVariable2;
很明显,myVariable 的类型为 int*,而 myVariable2 的类型为 int.在
It seems obvious that myVariable has type int*, while myVariable2 has type int.In
int* myVariable, myVariable2;
很明显,两者都是 int* 类型,但这并不正确,因为 myVariable2
具有 int 类型.
it may seem obvious that both are of type int*, but that is not correct as myVariable2
has type int.
因此,第一种编程风格更直观.
Therefore, the first programming style is more intuitive.
这篇关于为什么星号在变量名之前,而不是类型之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!