Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 8, 2007, 11:49 AM   #1
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Return two values (doubles) from a C function

I know I'm being really dumb here, but what's the best way of returning two numbers from a C-function back to the main method?
Eraserhead is offline   0 Reply With Quote
Old Feb 8, 2007, 12:17 PM   #2
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
There's no way to return more than one value.

The solution is to pass a pointer to a variable into your function, and set the value at that address in the function.

For example:

Code:
void main()
{
	double firstDouble, secondDouble;
	firstDouble = function(&secondDouble);
	// etc.
}

double function(double *doubleRef)
{
	*doubleRef = 2.0;
	return 1.0;
}
Nutter is offline   0 Reply With Quote
Old Feb 8, 2007, 12:25 PM   #3
Eraserhead
Thread Starter
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by Nutter View Post
There's no way to return more than one value.

The solution is to pass a pointer to a variable into your function, and set the value at that address in the function.

For example:

Code:
void main()
{
	double firstDouble, secondDouble;
	firstDouble = function(&secondDouble);
	// etc.
}

double function(double *doubleRef)
{
	*doubleRef = 2.0;
	return 1.0;
}
I was missing the *doubleRef in the function, now that's sorted it works thanks!
Eraserhead is offline   0 Reply With Quote
Old Feb 8, 2007, 12:45 PM   #4
mbabauer
macrumors regular
 
Join Date: Feb 2006
Another solution...

The other solution would be to create a data structure with two doubles in it. You could do something like

Code:
typedef struct {
    double one;
    double two;
} DoubleDouble;
I am a little rusty at C, but this is the basic idea and should work. In this case, you would get a reusable structure type defined to DoubleDouble.
__________________
mbabauer
8-Bits me!
http://www.codechimp.net
mbabauer is offline   0 Reply With Quote
Old Feb 8, 2007, 12:48 PM   #5
notjustjay
macrumors 603
 
notjustjay's Avatar
 
Join Date: Sep 2003
Location: Canada, eh?
From a semantics point of view, I would prefer to pass both variables by reference, or return a struct containing the two, rather than return one and pass the other by reference. It just seems cleaner this way.

Heh, DoubleDouble. That's how I take my coffee.
__________________
.
notjustjay is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Thread function that returns a value bluehill iPhone/iPad Programming 1 Jun 24, 2011 05:20 AM
AppleScript: Return a value to the Terminal? RachitGupta Mac Programming 9 Dec 16, 2009 02:45 PM
UITableViewCell Selection, Return Boolean value trojanvillage iPhone/iPad Programming 0 Oct 18, 2009 10:40 AM
Using an Objective-C array with a slider to return custom values generates warning StevenHu iPhone/iPad Programming 9 Oct 8, 2009 12:17 PM
function takes string parameter and returns string value vidyareef iPhone/iPad Programming 3 Jan 21, 2009 04:50 PM


All times are GMT -5. The time now is 02:47 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC