Friday, April 8, 2011

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.

From stackoverflow
  • As I said in my answer of your previous question, @"yep" is already a pointer, so there is no need of * before myString which is also a pointer. In this case, you assign pointers not values.

  • 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* fool // declare foo, a pointer to a Foo object
    &foo      // the memory address of foo
    *foo      // de-reference the pointer - gives the Foo object (value)
    
  • mmattax well covered the distinction between declaration (as a pointer) and dereferencing.

    However, as to your point about:

    
      (*myVar).myStructComponentX = 5;
    

    to access a member of an instance of a C struct (as this is) you can do what you did , or more commonly you use the -> notation:

    
      myVar->myStructComponentX = 5;
    

    Objective-C is a little confusing here because it recently (in ObjC 2.0) introduced property syntax, which is a short cut for:

    
      int val = [myObject someIntProperty];
    

    and can now be written as:

    
      int val = myObject.someIntProperty;
    

    This is Objective C (2.0) syntax for accessing a property which you have declared (not an actual member variable), whereas your example was accessing a member of a C struct.

    Make sure you are clear on the difference.

  • Not an answer, but a question: .h file:

    ...
    UIWebView *sections;
    UIWebView *feature;
    UIWebView *teasers;
    ...
    

    .m file:

    ...
    UIWebView *webView = nil
    ...
    if([section isEqualToString:@"Feature"]) {
        // Do I use
        webView = feature;
        // Or do I use
        webView = &feature;
        // Or do I use
        webView = *feature;
    }
    ...
    

    So in the above fragments, if I want to interact with webView as if it were feature, anything done to webView affects feature, which one do I do?: feature, *feature, or &feature?

    Sven : You should have asked a new question for this. But anyways, you use the first variant. You assign a pointer to another pointer. Second and third are errors: pointer = pointer-to-pointer or pointer = object don’t make sense.

0 comments:

Post a Comment