开发者

Is there an Ultimate Guide to Pointers in Objective-C? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_运维技巧

Closed 4 years ago.

Improve this question

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.


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

Take a look at here.


(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;.)


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜