Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Pointers in Objective-C
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Raz0rEdge" data-source="post: 1042771" data-attributes="member: 110816"><p>In the realm of C, you would use pointers to pass it's reference to a function as opposed to the value itself..</p><p></p><p>Your example above, (although buggy) is how you'd do it in C. The problem is that numberTwo has no "storage" that it's pointing to, so when you assign 14 to it, that becomes the "storage" and later one if you try to de-reference the pointer, the program will crash.</p><p></p><p>Within Objective-C, you still use pointers and you have to to be able to send messages to the class object..</p><p></p><p>For example:</p><p>[CODE]</p><p>Fraction *myFraction = [[Fraction alloc] init];</p><p>[myFraction somemethod];</p><p>[/CODE]</p><p></p><p>The [] syntax in Obj-C is doing a bunch of work here..in the first line, it will call your Fraction class' alloc function, followed by init function. What is returned is a instance of the object fully initialized. You can now call other methods within that class with the second line and pass in any arguments and so on.</p><p></p><p>Pointers are very useful constructs..so much so that certain languages make EVERYTHING a pointer (visibly or not)..but there're also a lot of easy pitfalls to get into..</p><p></p><p>Regards</p></blockquote><p></p>
[QUOTE="Raz0rEdge, post: 1042771, member: 110816"] In the realm of C, you would use pointers to pass it's reference to a function as opposed to the value itself.. Your example above, (although buggy) is how you'd do it in C. The problem is that numberTwo has no "storage" that it's pointing to, so when you assign 14 to it, that becomes the "storage" and later one if you try to de-reference the pointer, the program will crash. Within Objective-C, you still use pointers and you have to to be able to send messages to the class object.. For example: [CODE] Fraction *myFraction = [[Fraction alloc] init]; [myFraction somemethod]; [/CODE] The [] syntax in Obj-C is doing a bunch of work here..in the first line, it will call your Fraction class' alloc function, followed by init function. What is returned is a instance of the object fully initialized. You can now call other methods within that class with the second line and pass in any arguments and so on. Pointers are very useful constructs..so much so that certain languages make EVERYTHING a pointer (visibly or not)..but there're also a lot of easy pitfalls to get into.. Regards [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Pointers in Objective-C
Top