How to return two values in a function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • istillshine@gmail.com

    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.
  • Barry Schwarz

    #2
    Re: How to return two values in a function?

    On Fri, 11 Apr 2008 21:23:52 -0700 (PDT), istillshine@gma il.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_p ointer, my_int_pointer) ;
    you would use
    foo(my_double_p ointer, &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

    Comment

    • istillshine@gmail.com

      #3
      Re: How to return two values in a function?

      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.

      Comment

      • santosh

        #4
        Re: How to return two values in a function?

        istillshine@gma il.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.

        Comment

        • pereges

          #5
          Re: How to return two values in a function?

          On Apr 12, 9:23 am, istillsh...@gma il.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_functio n()
          {
          double *x;
          int *ret;

          foo(&x, &ret);

          }

          Also read FAQ 4.8

          Comment

          • Richard Heathfield

            #6
            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

            Comment

            • Bartc

              #7
              Re: How to return two values in a function?

              istillshine@gma il.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


              Comment

              • John Bode

                #8
                Re: How to return two values in a function?

                On Apr 11, 11:23 pm, istillsh...@gma il.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.

                Comment

                • Kenny McCormack

                  #9
                  Re: How to return two values in a function?

                  In article <d3e9028c-c5c6-4d96-bf59-95b099d1bdbd@a1 g2000hsb.google groups.com>,
                  John Bode <john_bode@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.

                  Comment

                  • Richard

                    #10
                    Re: How to return two values in a function?

                    gazelle@xmissio n.xmission.com (Kenny McCormack) writes:
                    In article <d3e9028c-c5c6-4d96-bf59-95b099d1bdbd@a1 g2000hsb.google groups.com>,
                    John Bode <john_bode@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.

                    Comment

                    Working...