Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2009, 02:59 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default NSString *Forename;..... why a Pointer?

Hi All

Everytime I declare a string object why can I only declare it as a Pointer?
e.g.

Code:
NSString *Forename;
Why can I not have a normal object?

Code:
NSString Forename;
Like an integer

Code:
int intAge;
In case you wondering, yes I am a vb programmer trying to learn C
rraagg is offline   Reply With Quote
Old 08-02-2009, 04:06 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Because that's the way the SDK works. Objects are allocated in heap memory, and you do all interaction with them via pointers to that object. Don't spend any time or energy trying to figure out "why", just learn to do it that way and move on.

joe
FlyingDiver is offline   Reply With Quote
Old 08-02-2009, 05:52 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I admire your curiosity. Other reasons you declare pointers to objects; maybe one of these will satisy:

The declarations are there to tell the compiler to allocate memory for variables, structures, etc. When you declare an int, the compiler know you need four bytes of space on the stack. When you declare a string, how much memory should the compiler reserve for that? 100 bytes? 1000 bytes?

You don't know how large a string should be. So instead, you declare a pointer to a string, and let the string class itself decide how much memory to needs (it will ask for its own memory on the heap, as Joe said.) All we need on the stack is a pointer to the object.

Also, dealing with strings on the stack - without pointers - would be a hassle. Every time you added a string to an array, you'd have to copy the whole string. When you get it back out of the array, you'd have a different copy. This is no big deal with primitives like ints and floats because any "seven" is as good as another, but with more complicated objects it's easier to just pass around the object's address (that's what a pointer is) instead of the entire object.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



» Advertisements
» Online Users: 471
14 members and 457 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0