c function returning multiple values

This is a discussion on c function returning multiple values within the C Programming forums, part of the General Programming Boards category; is it possible to return multiple values from a c function ? generally we can return a structure having multiple ...

  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,789
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    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, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Replies: 1
    Last Post: 02-03-2005, 02:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21