星号有时在变量类型上,有时在变量名称上.为什么? [英] Asterisk sometimes on variable type, sometimes on variable name. Why?

查看:199
本文介绍了星号有时在变量类型上,有时在变量名称上.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在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;

其他人更喜欢将其想象为*tweetPickerUIPickerView,也就是说,取消引用指针将得到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)程序员可能会说它们是相同的",但事实并非如此.在第一种情况下,bc是正整数,在第二种情况下,它们是指针.在两种情况下,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆