星号*在Objective-C中是什么意思?
是真的吗,星号总是表示嘿,那是指针!" 指针总是拥有内存地址?
Is it true, that the Asterisk always means "Hey, that is a pointer!" And an Pointer always holds an memory adress?
(是的,我知道*是用于数学运算的例外)
(Yes I know for the exception that a * is used for math operation)
例如:
NSString* myString;
或
SomeClass* thatClass;
或
(*somePointerToAStruct).myStructComponent = 5;
我觉得在定义作为类指针的变量时,我需要了解的更多关于Asterirsk(*).
I feel that there is more I need to know about the Asterirsk (*) than that I use it when defining an Variable that is a pointer to a class.
因为有时我已经在参数的声明中说过Parameter变量是一个指针,但仍然必须在Variable前面使用星号才能访问该值.我最近想以[myObj myMethod:& myStruct]的方式将结构的指针传递给方法之后,即使我的方法声明已经说过有一个参数(DemoStruct *)myVar确实应该是该演示程序的指针,但我仍然总是不得不说:伙计,编译器.听着!它是IIISSS指针:",并写成:(* myVar).myStructComponentX = 5 ;
Because sometimes I already say in the declaration of an parameter that the Parameter variable is a pointer, and still I have to use the Asterisk in front of the Variable in order to access the value. That recently happened after I wanted to pass a pointer of an struct to a method in a way like [myObj myMethod:&myStruct], I could not access a component value from that structure even though my method declaration already said that there is a parameter (DemoStruct*)myVar which indeed should be already known as a pointer to that demostruct, still I had always to say: "Man, compiler. Listen! It IIISSS a pointer:" and write: (*myVar).myStructComponentX = 5;
我真的真的不明白为什么我不得不重复两次.而且只有在这种情况下.
I really really really do not understand why I have to say that twice. And only in this case.
当我在NSString * myString的上下文中使用星号时,我可以按自己喜欢的方式访问myString,而不必每次都告诉编译器它是一个指针.即喜欢使用* myString = @是".
When I use the Asterisk in context of an NSString* myString then I can just access myString however I like, without telling the compiler each time that it's a pointer. i.e. like using *myString = @"yep".
对我来说这毫无意义.
一个*实际上是取消引用指针的运算符.唯一的意思是嘿,我是指针"是在变量声明期间.
an * is actually an operator to de-reference a pointer. The only time it means "hey i'm a pointer" is during variable declaration.
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)