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

Similar Threads
thread Thread Starter Forum Replies Last Post
Ducky Doubles - Version 2.0 - Free DustinKirk iPhone and iPod touch Apps 0 Apr 18, 2014 03:21 PM
Global function -> normal function? DennisBlah iPhone/iPad Programming 6 Feb 28, 2014 01:40 AM
Eliminating doubles in iTunes rockinrocker Mac Applications and Mac App Store 3 Nov 8, 2013 02:49 PM
Method return values childoftheko4n iPhone/iPad Programming 8 Jul 11, 2013 07:55 AM
Apple More Than Doubles Capital Return Program to $100 Billion MacRumors MacRumors.com News Discussion 68 Apr 29, 2013 06:35 AM

Forum Jump

All times are GMT -5. The time now is 06:49 PM.

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

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