Thread: Returning two variables?

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

    Returning two variables?

    Hey, Been a while since I posted here. Right now i'm designing a program that checks a socket and see's if the webpage is down or not. If its down i'm going to launch the application. However i'd like it to be more flexible, so i'm working on loading a "config" file into the program containing the address, port and path to the program.

    I'd like it to be a seperate function, but i'm curious how I could pass all that info back into the main function. This is my code so far, and i've got as far as the return call, but I don't think you can use multiple returns for each value. So i'm kind've stumped. This is fairly routine stuff i'm sure, but i'm self-taught. So the simple things are usually what gets me.

    Code:
    void load_config( void )
    {
    	FILE_DATA * fp;
    	int cnt;
    	char buffer[5000];
    	char * host;
    	char * path;
    	int port;
    
    
    	if (( fp = fopen("config.txt", "rb" )) == NULL )
    	{
    		perror("no config found.");
    	    return 0;
      	}
    
      	for ( cnt = 0; cnt < 3; cnt++ )
      	{
    		while ( buffer = fgetc( fp ) ) != EOF )
    		{
    			host = str_dup( buffer );
    			port = str_dup( buffer );
    			path = str_dup( buffer );
    		}
    	}
    
    	how to return the info?
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Something like
    Code:
    typedef struct info {
      char *host;
      int port;
      char *path;
    } info;
    
    void load_config( info *answer ) {
      // your code
      answer->port = port;
      // etc etc
    }
    
    int main ( ) {
      info answer;
      config( &answer );  // get multiple results stored here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Salem. Thanks for the reply. So basically i'd have to make an array to store the data, and then process it. Thanks for the help, and if i'm not mistaken. Ya used to help me back when I posted here years ago. Glad to see you're still around. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  2. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. Returning Variables
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2002, 02:37 PM