Asterisk*在Objective-C中的含义是什么?

Tha*_*nks 24 pointers objective-c

这是真的,Asterisk总是意味着"嘿,这是一个指针!" 指针总是有一个记忆地址?

(是的我知道a*用于数学运算的例外)

例如:

NSString* myString;
Run Code Online (Sandbox Code Playgroud)

要么

SomeClass* thatClass;
Run Code Online (Sandbox Code Playgroud)

要么

(*somePointerToAStruct).myStructComponent = 5;
Run Code Online (Sandbox Code Playgroud)

我觉得在定义一个指向类的指针的变量时,我需要了解更多关于Asterirsk(*)的信息.

因为有时我已经在参数声明中说过Parameter变量是一个指针,但我仍然需要在变量前面使用Asterisk来访问该值.最近发生在我希望以类似[myObj myMethod:&myStruct]的方式将结构的指针传递给方法之后,即使我的方法声明已经说过有参数,我也无法从该结构访问组件值( DemoStruct*)myVar确实应该被称为指向该demostruct的指针,我仍然总是说:"人,编译器.听!它IIISSS指针:"并写:(*myVar).myStructComponentX = 5;

我真的真的不明白为什么我要说两次.只有在这种情况下.

当我在NSString*myString的上下文中使用Asterisk时,我可以只访问myString但是我不喜欢每次都告诉编译器它是指针.即喜欢使用*myString = @"yep".

这对我来说毫无意义.

mma*_*tax 27

an*实际上是一个取消引用指针的运算符.唯一一次它意味着"嘿我是指针"是在变量声明期间.

Foo* foo  // declare foo, a pointer to a Foo object
&foo      // the memory address of foo
*foo      // de-reference the pointer - gives the Foo object (value)
Run Code Online (Sandbox Code Playgroud)

  • 它并不意味着在声明过程中有任何不同--`Foo*foo`实际上是`Foo*foo`,因为"`*foo`是一个'Foo`".这完全一致. (5认同)