2

Ok... I understand what pointers are and how they point to a memory location where the variable is stored.

However, I still can't fully get my head wrapped around when the pointer (asterisk) is to be used and when it isn't. It's still seemingly random to me.

Does anyone have any recommendations for good tutorials on the web or book chapters that really go into detail on pointers?

Thanks!

Edit: What I'm really looking for is a detailed guide that is specific to Objective-C either in a deep website tutorial or book chapter(s). I don't think there is enough space here to fully explain it for me.

sayguh
  • 2,540
  • 4
  • 27
  • 33
  • Possible duplicate: http://stackoverflow.com/questions/529482/objective-c-pointers with a very good answer by InverseFalcon. – jules Aug 05 '11 at 13:42
  • There some good info there, but it is not a duplicate for the question I have asked – sayguh Aug 05 '11 at 14:51

5 Answers5

3

Being aware that this is tagged as Objective-C..

Take a look at here.

ntl0ve
  • 1,896
  • 3
  • 19
  • 25
  • I know Obj-C is a superset of C but I'm looking for a guide more specific to Objective-C (and Cocoa). Examples like why NSLog(@"myString is equal to %@", myString); uses "myString" instead of "*myString" or "&myString" – sayguh Aug 05 '11 at 13:55
  • @sayguh: That's because `%@` instructs the formatter to grab the pointer at that address and send the `-description` message to it, then format the content of the resulting string at that point. `%@` with `object` is like `%s` with `[[object description] UTF8String]`. – Jeremy W. Sherman Aug 05 '11 at 16:18
2

(Un)fortunately, the asterisk * has many different meanings depending on context. So, let's look at the relevant ones. Let us write T for some arbitrary type (say int):

T x;     // variable of type x, stored somewhere in memory

T * pt;  // a pointer to a variable of type x -- doesn't have a value just yet

pt = &x; // the value of pt is now the address of the variable x

So far so good. We have used the asterisk to designate a new type, namely T*, which is a "pointer to T".

But what do we do with a pointer? We can dereference it to get to the value of the variable at the address pointed to by the pointer:

T y;      // another variable of type T
y = *pt;  // equivalent to y = x;

*pt = 81; // equivalent to x = 81;

So, if the asterisk is part of the typename, then it designates a pointer type. If it comes before a variable name (which is itself of pointer type), then it dereferences the pointer.

[Clarification:] In C, pointers naturally go hand-in-hand with the "address-of" operator &, which is used to actually obtain a pointer to something. In Obj-C, a pointer is obtained as the result of object allocation (+ initailization): T * pt = [T new];

Beware that Obj-C offers an alternative syntax to the traditional C and C++ pointer syntax, so you may encounter pointers in other guises.[/Clarification]

(The asterisk can also be used as a binary operator of course, so you can have something like this: int x = 5; int * p = &x; int y = *p * *p;.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • He's talking about Obj-C. It's totally different. All objects (instances of classes) are pointers there ... – cutsoy Aug 05 '11 at 12:58
  • And Obj-C has no reference types like C++. It is pure C with OO, Smalltalk-like, extensions. – Rudy Velthuis Aug 05 '11 at 13:01
  • @Rudy: I'm not mentioning references at all. (Indeed, in C++ a dereference would be of type `T&`, but I'm not saying this here.) Tim: Isn't Objective-C a superset of C? All the C pointer mechanics should apply there as well? If not I'll delete this post. – Kerrek SB Aug 05 '11 at 13:02
  • Though you can use the `&`-symbols as `pass-by-reference`-flags, and specify them in the receiving method as `NSError **`, we (almost?) never use `*` and `&` for any other purpose. This is the way we initialize objects: http://en.wikipedia.org/wiki/Objective-C#Instantiation – cutsoy Aug 05 '11 at 13:13
  • @Kerrek SB: Great, however, C# is something entirely different from Obj-C :P! C = C Sharp, made by Microsoft for developing Windows-apps. – cutsoy Aug 05 '11 at 13:30
  • @Tim: D'oh, sorry, fixed! I had C# in my mind from a recent interview I'm afraid :-) Do feel free to edit my post if you want to add some details. – Kerrek SB Aug 05 '11 at 13:34
  • Your last example dereferences a non-pointer integer `x` twice! Please change that to `int y = *p * *p` or some other safe, semantically correct configuration. – Richard Aug 05 '11 at 13:41
  • I appreciate this Kerrek and would vote helpful if I had the rep to do it but I'm more specifically looking for a great guide specific to the Objective-C superset. As you mentioned there is much alternative syntax and that is the part I'm really trying to understand – sayguh Aug 05 '11 at 13:58
  • @Sayguh: Indeed please do look for something Obj-C specific -- I was just thinking that you might have some fundamental uncertainties about what pointers are and that this might shed some light, but of course you should concentrate on the idiomatic Obj-C way. Good luck! – Kerrek SB Aug 05 '11 at 14:05
2

Peter Hosey of Growl and Adium has posted a guide to pointers on his blog. He calls it, "Everything you need to know about pointers in C," but that's because he regards the ugly pointer aspects as belonging to C rather than Obj-C. Check it out.

I don't know of any reference beyond the Clang ARC reference that describes all the crazy modifiers you can now put on pointers in Obj-C, though. And that's not a good learning resource if you're already confused.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
1

Assuming that C pointers have no secret to you, basically in Objective-C there are two caveats to avoid:

  • The NS- prefix does not always denote a class. Sometimes this is only a typedef. You have to check the reference manuals.

  • Dotted notation introduced in Objective-C 2.0. Despite the fact that every object is a pointer, sometimes instance variables can be accessed by myObj.myVar, instead of (more consistent for newcomers) myObj->myVar.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
0

The ultimate guide to pointers is in Kernighan & Ritchie. They're the same in Objective-C as they are in C.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • Instances in Objective-C are totally different from instances in C++. In Objective-C, objects always get a asterisk. – cutsoy Aug 05 '11 at 13:15
  • 1
    NSString* is just a pointer, a pointer similar to char* except that they point to different types. – progrmr Aug 05 '11 at 13:23
  • K&R won't begin to address the complexities of a declaration like `__weak __block MyClass *me`, nor will it discuss implicit coercion from `id` to any other object type. It also doesn't cover `restrict`, which has been part of standard C since 1999 or so. – Jeremy W. Sherman Aug 05 '11 at 22:54