New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Pointers in Objective-C


Post Reply New Thread Subscribe

 
Thread Tools
allyouhave

 
Member Since: Dec 30, 2009
Posts: 4
allyouhave is on a distinguished road

allyouhave is offline
I'm just now teaching myself how to code in Objective-C (however, I have no background in C or any other extension), and I'm having some trouble understanding pointers. While reading around, I noticed a lot of people explaining that all objects are just pointers referencing objects. If this is true, then why do I ever have to explicitly create pointers? What is the difference between the follow two statements?

int numberOne = 12;
int *numberTwo = 14;

From what I remember from Java, it would follow that I could do numberOne = numberTwo, and therefore both are equal to 14. I can, however, still change either number individually. i.e. numberTwo = 17 (and therefor numberOne = 14 and numberTwo = 17). Of course, if I were to do the opposite, then the pointer numberTwo would point to the same object as numberOne, and every time I changed one of them, "both" would change.

But if all variables are actually just pointers, then aren't these two phrases exactly the same?

numberOne = numberTwo;
or
numberTwo = numberOne;

I have about five ebooks on Cocoa or Objective-C, and I'm only three chapters into my first book, so if any of you can reference a book or website for me to check out, or just shed some light on my problem with understanding, that would be faaantastic.
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,699
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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];
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 Thanks
systix

 
Member Since: Dec 09, 2008
Posts: 19
systix is on a distinguished road

systix is offline
I don't know objective-c, i aim to learn though. In C however a good example of the use of pointers is a function that swaps two values. eg

Code:
voide main()
{
    int val1, val2;
    swap (val1, val2);
}

int swap (int val1, int val2)
{
     int temp = val1;
     val2 = val1;
     val1 = temp;
}


This will not work. The values are copied into the swap() function and swapped within the scope of the function, but not externaly to it. However, if we pass in pointers to the original variables we can indirectly access them and the swap will occure on the original values.

I hope this helps!
QUOTE Thanks

Post Reply New Thread Subscribe


« installer builder for OS X | Generating Keyboard Events »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
why classes and objects are similar in objective c harsha973 OS X - Development and Darwin 2 05-11-2010 08:12 PM
dynamic mwmory allocation in objective c drewangel OS X - Development and Darwin 1 03-17-2010 02:45 AM
Has to be an easier way to deal with Time in Objective C fajhetti OS X - Development and Darwin 2 02-03-2010 12:43 PM
Objective C Books that don't need C knowledge J-Mak OS X - Development and Darwin 4 10-27-2009 01:31 AM
question about objective C memory allocation thuffir OS X - Development and Darwin 3 11-18-2008 11:30 PM

All times are GMT -4. The time now is 03:18 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?