• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: returning value in a function argument
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: returning value in a function argument


  • Subject: Re: returning value in a function argument
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 17 May 2006 21:37:25 -0400

On May 17, 2006, at 8:09 PM, Angelo Chen wrote:

How to return NSString in a function argument?
following code does not work:

void getInfo(NSString *info)
{
	info = @"my info";
}

later in the code:

	NSString *info;
	getInfo(info);

but info has nothing

You need to pass by reference:

// Note that info is *not* an NSString* - it's a pointer to a NSString*. A
// pointer to a pointer.
void getInfo(NSString **info)
{
// First, you have to check to see if info is NULL
if (NULL != info)
{
// Since info is a NSString**, what we get
// back by dereferencing is a NSString*.
*info = @"my info";
}
}


Later on...

NSString *myInformation;

// Use & to pass the address of myInformation - that is, a pointer to the pointer
getInfo(& myInformation);


I don't know why so many folks are suggesting you return an array instead. Return by reference is a perfectly cromulent pattern, and it's been used for decades to work around C's insistence on a single return value. Far from discouraging it, Apple is adopting it all over the place with NSError objects.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: returning value in a function argument
      • From: Scott Ellsworth <email@hidden>
References: 
 >returning value in a function argument (From: Angelo Chen <email@hidden>)

  • Prev by Date: Re: Making newly inserted row visible in NSTableView
  • Next by Date: CoreData vs. SQLite for Multi-User Simultaneous Access
  • Previous by thread: Re: returning value in a function argument
  • Next by thread: Re: returning value in a function argument
  • Index(es):
    • Date
    • Thread