What does the Asterisk * mean in Objective-C?

Posted by Thanks on Stack Overflow See other posts from Stack Overflow or by Thanks
Published on 2009-02-24T13:19:27Z Indexed on 2010/06/12 17:13 UTC
Read the original article Hit count: 151

Filed under:
|
|

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;

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".

It just makes no sense to me.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about pointers