How to get more than one value from function in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kirankher
    New Member
    • Sep 2010
    • 2

    How to get more than one value from function in c

    Hi,

    I want a function in c than can return more than one integer values in array passed as argument.

    From main function i m calling a function (say foo)with array as argument. I want foo to return set of values in array which was passed as arg. foo should allocate memory for each array element.

    please help me.

    Kiran.
  • manontheedge
    New Member
    • Oct 2006
    • 175

    #2
    I believe what you are looking for is passing by reference. You need to pass the array into the function by REFERENCE, instead of by VALUE. Basically, this allows you to change the array passed into a function, which when returning to Main(), the array you originally passed into the function will be updated. This works because when you pass by reference, you're working directly with the array in memory, instead of a copy of it. You can't return multiple values from a function. But when you want to, this is how it's done.

    The other option is making the array global, but most people frown on that.

    Comment

    • hype261
      New Member
      • Apr 2010
      • 207

      #3
      Aren't all arrays passed by reference in C?

      By Kirans original statement, foo should allocate memory for each arry element, I believe he is looking for more of a pointer to a pointer or a reference to a pointer so that he can pass a pointer in and what it is pointing at will be changed at the end of the function. Somthing like this could work.

      void foo(int * & ptr, int & numberOfElement s)

      numberOfElement s tracks how many elements where created inside the function so you don't go out of bounds later.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The question is not making sense.

        Arrays are passed to functions typically by using the array name. The array name is aleays the address of element 0.

        Therefore, when you update array values inside the function you are actually updatuing the array that's outside the function.

        As to returning multiple values (assuming using the return keyword), you must return a type ot a pointer to a type. Therfore, multiple returned values need to be in a struct or union (or if C++ struct, union or class).

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Here is a function which returns more than one value.
          Code:
          void computeCircle(double& area, double& circumference,double r)
          //returns the area and circumference of a circle wih
          //radius r
          {
          area = PI*r*r;
          circumference = 2*PI*r;
          }

          Comment

          • Kirankher
            New Member
            • Sep 2010
            • 2

            #6
            Hi all,

            Thanks for your help. I got what i wanted.

            Below is a snippet:

            Code:
            int main()
            {
             int b, num;
             int *array;
            
             b=func(&array, &num);
            
             for (b=0; b<=num; b++)
              {
                printf("%d\n", array[b]);
              }
            return 0;
            }
            
            int func (int **array, int *num)
            {
            int i;
            int *p;
            for (i=0; i<4; i++)
            {
            	p = (int *)realloc(p,(i * sizeof(int)));
            	p[i] = i+5;
            }
            
            //Return address of constructed array
            *array=p;
            
            //Return no of elements to ensure we r not crossing over
            *num=i-1;
            
            return 0;
            }

            Comment

            Working...