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 > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-21-2009, 05:17 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 39
Arcadiu is on a distinguished road
Default Pointer Question

Sorry, I'm back already. I've just been reading this book on Obj C which was recommended by a few people here on the forums and I have come across a problem in the following code

Code:
#import <Foundation/Foundation.h> 
int main (int argc, const char * argv[]) 
{ 
  const char *words[4]  // What purpose does that pointer bring? Why do i need it?
    = {  "Joe- Bob \"Handyman\" Brown", 
      "Jacksonville \"Sly\" Murphy", 
      "Shinara Bain", 
      "George \"Guitar\" Books" }; 
  int wordCount = 4; 
  int i; 
  for (i = 0; i < wordCount; i++) { 
    NSLog (@"%s is %d characters long", 
        words[i], strlen(words[i])); 
  } 
  return (0); 
} // main
I just am wondering why i need a little asterisk, and what am i pointing the array of words to? This is my real taste of OOP ever and the book I'm reading hasn't made any comment about that asterisk when it walks through the code?

Thanks for your time

Arc
Arcadiu is offline   Reply With Quote
Old 10-21-2009, 11:35 AM   #2 (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

Let me try two ways -

(1) The "just because" - strlen expects you to pass a pointer, not the actual data, so you must pass a pointer.
strlen() - Standard C String & Character - C Programming Reference - eLook.org

(2) Pointers are addresses. When dealing with strings (and later with objects) it's easier to pass the address of the object, which is only four bytes, instead of copying the whole string or object.

This also has another advantage; if you have the address of the original string or object, you can make changes to that string or call methods on that object and they'll be reflected in the original. If you passed strings or object by value, the way that ints and floats usually are, then you would not be able to affect the original.

The function strlen() doesn't change the contents of the string you pass, but some other functions do; it's an important use of pointers. Check out this answer for more details:

parameter passing: pass by value and pass by refernce

If you understand that, you probably understand pointers.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 10-21-2009, 01:21 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 39
Arcadiu is on a distinguished road
Default

wow thankyou very much you have been very helpful so far on this forum I am sure to remember your name comrade smasher
Arcadiu 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: 465
13 members and 452 guests
Domele, Duncan C, Feldspar, MacBook MH, Objective Zero, patapple, peterwilli, pipposanta, PixelInteractive, Punkjumper, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

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