星号有时在变量类型上,有时在变量名称上.为什么? [英] Asterisk sometimes on variable type, sometimes on variable name. Why?
问题描述
例如,在Objective-C中,我已经看到:
In Objective-C, I've seen, for example:
UIPickerView *tweetPicker
和
UIPickerView* tweetPicker
星号是什么意思(我知道,第一部分是假的...),为什么它在不同的上下文中出现在声明的不同部分?
What does the asterisk mean (I know, the first part is a dupe...) and why does it go on different parts of the declaration in different contexts?
推荐答案
在您所显示的确切情况下,没有什么区别.有些人喜欢将tweetPicker
定义为UIPickerView *
类型,即指向UIPickerView
的指针.这些人通常将其写为
In the exact case you're showing, there is no difference. Some people like to think of tweetPicker
being of type UIPickerView *
, that is, a pointer to a UIPickerView
. These people usually write it as
UIPickerView* tweetPicker;
其他人更喜欢将其想象为*tweetPicker
是UIPickerView
,也就是说,取消引用指针将得到UIPickerView
.这些人通常写:
Other people prefer to think of it like *tweetPicker
is a UIPickerView
, that is, dereferencing the pointer gives a UIPickerView
. Those people usually write:
UIPickerView *tweetPicker;
我更喜欢后者,因为C(和Objective-C)语法更好地支持了它.以下面的变量声明为例:
I prefer the latter, because the C (and Objective-C because of that) syntax supports it better. Take, for example, the following variable declarations:
int* a, b, c;
int *a, *b, *c;
乍看之下,新手C(或Objective-C)程序员可能会说它们是相同的",但事实并非如此.在第一种情况下,b
和c
是正整数,在第二种情况下,它们是指针.在两种情况下,a
都是指针.
At first glance, the novice C (or Objective-C) programmer might say "those are the same", but they're not. In the first case, b
and c
are regular integers, in the second case, they're pointers. a
is a pointer in both cases.
从我的角度来看,无论如何,类型"的概念在C语言中还是很弱的(与类型转换等行为有关),以至于将该概念进一步扩展到指针变量是疯狂的-尤其是对于自动&在您获得的void *
或id
之间进行无声转换.
From my perspective, the concept of a "type" is so weak in C anyway (what with the behaviour of typecasting and the like) that extending that concept one step further to pointer variables is crazy - especially with the automatic & silent conversions to and from void *
or id
that you get.
这篇关于星号有时在变量类型上,有时在变量名称上.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!