473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to return two values in a function?

double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;
}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
Jun 27 '08 #1
9 1674
On Fri, 11 Apr 2008 21:23:52 -0700 (PDT), is*********@gmail.com wrote:
>double *foo(double *x, int *ret)
{
double *x;
You don't want two definitions of x.
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
One common method is to change the function definition to
double *foo(double *x, int **ret)
and within the function change ret to *ret.

In the calling function, instead of calling the function with
foo(my_double_pointer, my_int_pointer);
you would use
foo(my_double_pointer, &my_int_pointer)

If you chose the method, I would do it for both arguments and change
the function to return nothing (void) or to return an int success
flag.
>
struct ret_val_t {
double *x;
int *ret;
}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
When you start to write complicated programs with many functions, you
will appreciate this advice.
Remove del for email
Jun 27 '08 #2
On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel.comwrote:
When you start to write complicated programs with many functions, you
will appreciate this advice.
Thanks.

Remove del for email
I don't understand this.
Jun 27 '08 #3
is*********@gmail.com wrote:
On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel.comwrote:
[ ... ]
>Remove del for email

I don't understand this.
What's not to understand? It means that you need to remove the three
characters: d, e, and l from his email address to get a valid address
to contact him.

Jun 27 '08 #4
On Apr 12, 9:23 am, istillsh...@gmail.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;

}

In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;

}

Try using double pointers. In the calling function don't pass the copy
of the pointer, instead pass the address of the pointer. Your pointer
will get modified.

void foo(double **x, int **ret)
{

}

void calling_function()
{
double *x;
int *ret;

foo(&x, &ret);

}

Also read FAQ 4.8
Jun 27 '08 #5
pereges said:

<snip>
Try using double pointers.
Because C has a data type 'double', the phrase 'double pointer' is
ambiguous. It is clearer to say 'pointer to pointer'. (But yes, I knew
what you meant, and yes, that's one way to solve his problem.)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #6
is*********@gmail.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
The advice you've received has been to return the values in the parameters,
so (to use a much simpler example):

#include<stdio.h>
void returnpair(int *a,int *b) {
*a+=100;
*b+=200;
}

int main(void) {
int a,b;
a=20;
b=30;
returnpair(&a,&b);
printf("A', B' = %d %d\n",a,b);
}

You need an extra level of pointers (hence the ** for your example). However
this doesn't address your question (and will also overwrite your
parameters).
struct ret_val_t {
double *x;
int *ret;
}
This is the natural solution; what's wrong with this?
One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
If it works, use it until you have time to do it better.

Another way is to return the parameters one at a time, but it could mean
calculating twice, depending on how you arrange your function, and you need
an extra parameter to select the return value:

#include<stdio.h>

int returnpair(int a,int b, int n) {
a+=100;
b+=200;
if (n==1) return a;
return b;
}

int main(void) {
int a,b,anew,bnew;

a=20;
b=30;
anew = returnpair(a,b,1); /* must not change a,b between calls */
bnew = returnpair(a,b,2);

printf("A', B' = %d %d\n",anew,bnew);
}

--
Bart
Jun 27 '08 #7
On Apr 11, 11:23 pm, istillsh...@gmail.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;

}

In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;

}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).

If the values you are returning naturally make up a composite type
(such as fields in a street address), use a struct to group them
together and return an instance of the struct. If the values you are
returning do not naturally make up a composite type (such as a buffer,
buffer length, and read status), use separate writable function
parameters.
Jun 27 '08 #8
In article <d3**********************************@a1g2000hsb.g ooglegroups.com>,
John Bode <jo*******@my-deja.comwrote:
....
>Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).
Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here.

Jun 27 '08 #9
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <d3**********************************@a1g2000hsb.g ooglegroups.com>,
John Bode <jo*******@my-deja.comwrote:
...
>>Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).

Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here.
Serious question for you - having read all Heathfield's bluster, is
there any way you could be convinced that C does not have "global
variables"? I know that I shake my head more and more when I read his
bullshit. What I want to know is , what the hell does he and the other
clique members think they are gaining by playing such silly games? I
have worked on more big C projects than I care to remember and not once
did any one get confused when "globals" where mentioned in the context
of the program. Why here in c.l.c? Really. My mind boggles at their self
important posturing in the face of common sense.

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
5
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
80
by: xicloid | last post by:
I'm making a function that checks the input integer and returns the value if it is a prime number. If the integer is not a prime number, then the function should return nothing. Problem is, I...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.