Thread: 2 returns?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    2 returns?

    i have a function that will need to return 2 varibles, a int and a char. how i could i assigned 2 varibles from this function?

    thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if your function needs to return 2 values you probably have a design flaw.
    You have several choices. Best is to pass your variables that need returning into function by reference i.e. as a pointer.
    alternatively pass address of a struct into function and store your 2 return values in struct.
    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

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i need to return 2 varibles for a arguement check. if the user doesnt eneter any on the command-line, they get a "shell" function from my program. i another function(next_word) would copy the string until theres a space for the arg handler. but i need to 2 vaules straight.

    Code:
    while(prompt[x] != '\0') {
         get_arg( next_word(prompt, x), '/0', 1) 
    
         switch( get_arg( next_word(prompt, x), '/0', 1) ) {
              /* some stuff here*/
         }
    }
    my function get_arg see if the first 1 arg is a flag, if it is it copy the 2nd arg to a varible that gets used near the end. It return 1 on error. But most of the time the program will only be ran with 1 arg, a filename. So i was thinking pass one arg to it and if it gives a error do it again with 2 args. Most of time it will only have one so this should be that bad.

    Any idea on how to do this or a better way?

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    A function can at most return one value. If you need to pass two or more values back use pointers and pass by reference.

    Mr. C.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    how? i dont see how that is doing anything but changing types.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    can any explain this?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    maybe if you were a bit clearer about what you are trying to achieve and bumping of threads here is viewed badly. punishments have included thread deletion and turning all your threads pink. You've been warned!
    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

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Using pointers to get values can be done like in the example.

    Code:
    /* Function to get values */
    void function (int *a, int *b)
    {
        ...
    }
    
    int main (void)
    {
        int a, b;
        ...
    
        /* Get values of a and b */
        function (&a, &b);
    
        /* Check values of a and b */
        check_val (a, b);
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    maybe be this is a better question, how would you handle taking command line arguemnts(like -t DATA) in a program(not from the command line)? I had a cheezy idea in mind but it doesnt seem like its a good idea(it doesnt even work actually). Anyone have some code from a program I can see?

    and thanks for the info Stoned_Coder!

  10. #10
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Use a struct with all of the variables you need to return and then just pass an instance of the struct. That way you only return one object, but a bunch of variables get changed. It also makes your function tag simpler.
    Code:
    typedef struct {
            char flag;
            char *value;
    } CMD_LINE;
    
    CMD_LINE get_args( char *argv[], CMD_LINE args )
    {
            /* Just faking what the function does. */
            if( argv[1][0] == '-' && argv[1][1] == 'f' ) {
                    args.flag = 'f';
                    args.value = "Format something";
            }
            return args;
    }
    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM