PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How to return two values in a function?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to return two values in a function?

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 05h23   #1
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 12/04/2008, 06h00   #2
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

On Fri, 11 Apr 2008 21:23:52 -0700 (PDT), istillshine@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
  Réponse avec citation
Vieux 12/04/2008, 14h00   #3
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel.com> wrote:

> 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.
  Réponse avec citation
Vieux 15/04/2008, 16h41   #4
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

istillshine@gmail.com wrote:

> On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel.com> wrote:


[ ... ]

>> 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.

  Réponse avec citation
Vieux 15/04/2008, 19h20   #5
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

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
  Réponse avec citation
Vieux 15/04/2008, 19h38   #6
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

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
  Réponse avec citation
Vieux 15/04/2008, 20h34   #7
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

istillshine@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


  Réponse avec citation
Vieux 15/04/2008, 22h27   #8
John Bode
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

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.
  Réponse avec citation
Vieux 15/04/2008, 22h37   #9
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

In article <d3e9028c-c5c6-4d96-bf59-95b099d1bdbd@a1g2000hsb.googlegroups.com>,
John Bode <john_bode@my-deja.com> wrote:
....
>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.

  Réponse avec citation
Vieux 16/04/2008, 10h50   #10
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to return two values in a function?

gazelle@xmission.xmission.com (Kenny McCormack) writes:

> In article <d3e9028c-c5c6-4d96-bf59-95b099d1bdbd@a1g2000hsb.googlegroups.com>,
> John Bode <john_bode@my-deja.com> wrote:
> ...
>>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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18442 seconds with 18 queries