Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Asterisk * mean in 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)

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.

like image 572
Thanks Avatar asked Feb 24 '09 13:02

Thanks


People also ask

What does the asterisk mean in Objective C?

it's how you make a pointer to something. the * means to dereference the pointer, if it's in code. if it's part of a declaration, it's simply indicating the variable is a pointer.

What does the symbol mean in Objective C?

That symbol is used to declare block. For more information read here Blocks Programming Topics. Some more info: Block objects are a C-level syntactic and runtime feature.

What does the asterisk mean in programming?

(2) In programming, the asterisk or "star" symbol (*) means multiplication. For example, 10 * 7 means 10 multiplied by 7. The * is also a key on computer keypads for entering expressions using multiplication.

What does a double asterisk mean in C?

It declares a pointer to a char pointer.


1 Answers

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) 
like image 157
mmattax Avatar answered Sep 30 '22 23:09

mmattax