作者:sunhuan | 来源:互联网 | 2023-06-03 02:06
Isittrue,thattheAsteriskalwaysmeansHey,thatisapointer!AndanPointeralwaysholdsan
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)
(是的,我知道一个*用于数学运算的例外情况)
For Example:
例如:
NSString* myString;
or
或
SomeClass* thatClass;
or
或
(*somePointerToAStruct).myStructCompOnent= 5;
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.
我觉得在定义一个指向类的变量时,我需要知道的关于星号(*)的更多信息。
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;
因为有时候我已经在参数声明中说过参数变量是指针,而且我还必须在变量前面使用星号来访问值。最近发生在我想要一个结构体的指针传递给一个方法在某种程度上像[myObj myMethod:&myStruct],我不能访问组件值结构尽管我的方法声明已经说过有一个参数(DemoStruct *)myvar#的确应该已经称为一个指针DemoStruct,仍然我总是说:“男人,编译器。听!它就像一个指针:“并且写:(*myVar)。myStructCompOnentX= 5;
I really really really do not understand why I have to say that twice. And only in this case.
我真的真的不明白为什么我得说两遍。只有在这种情况下。
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".
当我在NSString* myString上下文中使用星号时,我可以随意访问myString,而不用每次都告诉编译器它是一个指针。例如使用*myString = @" yes "。
It just makes no sense to me.
这对我来说毫无意义。
4 个解决方案