functions and return

This is a discussion on functions and return within the C Programming forums, part of the General Programming Boards category; I think the answer is a no but want to confirm it. What I'm looking to do is create a ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    functions and return

    I think the answer is a no but want to confirm it.

    What I'm looking to do is create a function that can return a string OR an int.

    psudo code:
    Code:
    function (int selector)
    {
    switch (selector)
    	{
    	case 0:
    	     <blah blah blah>
    	     return ("IAmAString");
    
    	case 1:
    	     <more blah blah blah>
    	     return (SomeInt);
    	}
    }
    If it is possible what would I put before the function()? I know for int its int function() and for a string char* function().

    Thanks

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    AFAIK, the only way to pass strings among the functions is to use structures or external variables.
    Last edited by cc0d3r; 08-11-2003 at 02:30 PM.
    $ENV: FreeBSD, gcc, emacs

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Originally posted by cc0d3r
    AFAIK, the only way to pass strings among the functions is to use structures or external variables.
    Here is a working function to show you how to pass strings between functions. Only global variable is player.status.

    Code:
    char* titles (void)
    {
    switch (player.status)
           {
           case 0:
                return ("Dirt Ball");
                break;
    
           case 1:
                return ("Serf");
                break;
    
           case 2:
                return ("Peasant");
                break;
    
           case 3:
                return ("Adventurer");
                break;
    
           case 4:
                return ("Governer");
                break;
                
           case 5:
                return ("Grand Governer");
                break;
    
           case 6:
                return ("Duke");
                break;
    
           case 7:
                return ("King");
                break;
    
           }
    }
    This works and compiles without any warnings or errors.

    BTW what the heck is AFAIK?
    Last edited by Thantos; 08-11-2003 at 02:39 PM.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    As Far As I Know
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ah.

    Guess I will have to find another way this.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is a bit of a guess, but might this be anything like what you're after?
    Code:
    #include <stdio.h>
    
    char *get_title(size_t selector)
    {
       static const char *title[] =
       {
          "Dirt Ball",
          "Serf",
          "Peasant",
          "Adventurer",
          "Governer",
          "Grand Governer",
          "Duke",
          "King",
       };
       if ( selector < sizeof(title)/sizeof(*title) )
       {
          return title[selector];
       }
       return NULL;
    }
    
    int main ( void )
    {
       size_t i;
       for ( i = 0; i < 10; ++i )
       {
          char *mytitle = get_title(i);
          printf("%lu - ", (long unsigned)i);
          puts(mytitle ? mytitle : "invalid");
       }
       return 0;
    }
    
    /* my output
    0 - Dirt Ball
    1 - Serf
    2 - Peasant
    3 - Adventurer
    4 - Governer
    5 - Grand Governer
    6 - Duke
    7 - King
    8 - invalid
    9 - invalid
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    @ Dave_Sinkula - that code was an example for cc0d3r to show how you can pass strings.

    @ Salem I think I understand what you did. Thanks for the answer. Will have to play with it to get down.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    couldn't you just return a pointer to an int, and just typecase it, kind of like this

    Code:
    int *function(int showint) {
         if(showint)
               return 2;
         else
               return "hi";
    }
    int main(void) {
        char hi[5];
        int hello;
    
        hi = (char *)function(0);
        hello = (int *)function(1);
        return 0;
    }
    What will happen with that code, if it will even compile.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Code:
       static const char *title[] =
       {
          "Dirt Ball",
          "Serf",
          "Peasant",
          "Adventurer",
          "Governer",
          "Grand Governer",
          "Duke",
          "King",
       };
    Get rid of the red comma from Dave's post.
    Away.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by blackrat364
    Get rid of the red comma from Dave's post.
    It makes no difference. You can have a trailing comma. It has no effect.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Salem's idea is the best IMO and was the one I was thinking of.

    Using a struct with a union and an indicator would be the easiest way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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