Thread: c function returning multiple values

  1. #1
    Unregistered
    Guest

    Question c function returning multiple values

    is it possible to return multiple values from a c function ?

    generally we can return a structure having multiple members

    but this is equivalent to returning a single item from a

    function .

    how to return more then one value from a c function ?


    avinna

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You can pass the variables by reference.... they are not really returned, but the effect is similar

    Code:
    #include <stdio.h>
    
    
    void doubleit(int &x, int &y){
    x *= 2;
    y *= 2;
    }
    
    int main(void){
    int x = 10;
    int y = 5;
    
    doubleit(x,y);
    
    printf("%i %i",x,y);
    
    return 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    And in C, this would be
    Code:
    #include <stdio.h>
    
    void doubleit( int *x, int *y){
       *x *= 2;
       *y *= 2;
    }
    
    int main(void){
       int x = 10;
       int y = 5;
       doubleit( &x, &y);
       printf("%i %i",x,y);
       return 0;
    }

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can also wrap up your return values in an array( if same type) or a structure ( if different types) and return that.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM