• 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: return two values at once from C function?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: return two values at once from C function?


  • Subject: RE: return two values at once from C function?
  • From: "Jonathan E. Jackel" <email@hidden>
  • Date: Fri, 29 Aug 2003 12:03:02 -0400

Very easy to do. Your method/function signature should ask for a pointer to
where the error code goes. In the case of an NSObject, this is actually a
pointer to a pointer. When you call the method/function, you provide the
address where the error code goes.

Example (pseudo code -- usual disclaimers):

- (int)divideThis:(int)this byThat:(int)that error:(NSString **)err
{
if (that == 0)
{
if (err)
*err = @"Division By Zero";
return 0;
} else {
return this/that;
}
}

Then you call it like this:

- (void)demo
{
int this = 6, that = 0, theOther;
NSString *errorCode;
theOther = [yourObject divideThis:this byThat:that error:&errorCode];
if ([errorCode isEqualToString:@"Division By Zero"])
//handle error
...
}

Jonathan

> Greetings again,
>
> I'm now wondering if I SHOULD make the function in my previous email
> handle the errors. The problem is...
>
> I've probably missed something really obvious, but I'm trying to figure
> out if there's any way to return more than one value from a function.
> In particular this is relevant when I'm trying to return an error code
> along with another integer or something.
>
> What if say, I have a function that returns an integer, but if there's
> an error, the function says to return null. Does that actually make
> any sense? Or is that breaking something, or whatever?
>
> Any thoughts on this matter would be most appreciated.
> Cheers!
> David.
> _______________________________________________
> cocoa-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >return two values at once from C function? (From: David Thorp <email@hidden>)

  • Prev by Date: Re: Dynamically added text views
  • Next by Date: Re: Dynamically added text views
  • Previous by thread: Re: return two values at once from C function?
  • Next by thread: RE: return two values at once from C function?
  • Index(es):
    • Date
    • Thread