Thread: Returning Variables

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Returning Variables

    I know I asked this question about a year ago but I came upon a simple solution that is probably common knowledge.

    Code:
         int retvar()
         {
              char retvar[]="retvar";
              return retvar;
         }
    
         int main()
         {
          retvar();
          cout << retvar;
          }
    whats wrong? when i put that in "int/void main it doesn't return the variable. And in advance, how would i go about returning multiple variables?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    don't you have to define the function in order to return a value?
    Last edited by indigo0086; 09-08-2002 at 10:13 AM.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    several problems...

    The function:
    Code:
    int retvar()
         {
              char retvar[]="retvar";
              return retvar;
         }
    declares an int return value then tries to return a char*. It also tries to return a char* which will point to nothingness - retvar is destroyed at the end of the function.

    In addition to this, main has no way of knowing about retvar. It has not been declared or referenced or anything anywhere.

    The following should accomplish what you are trying to do - I hope
    Code:
    #include <iostream.h>
    
    void retvar( char[] charArray, int arraySize )
    {
       charArray = "cat";
    }
    
    int main( )
    {
       char array[ 100 ];  //100 characters in an array
       retvar( array, 100 );
       cout << array << endl;
    }
    Arrays are passed by reference so there is no need to return the array.

    This probably isn't very clear, just shout if you have questions
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: several problems...

    Originally posted by endo

    The following should accomplish what you are trying to do - I hope
    Nope.

    I'll make a try:

    Code:
    char* retvar( char* charArray, int arraySize )
    {
       strncpy(charArray,"cat",arraySize-1);
       charArray[arraySize-1]='\0';
       return charArray;
    }
    
    int main( )
    {
       char array[ 100 ];  //100 characters in an array
       cout << retvar( array, 100 ) << endl;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    It looks like you were trying to display a variable, retvar, that was out of scope. The variable retvar is specific to the retvar() function only, it can't be used anywhere else. Not only that, you made an integer-returning function that is returning a char variable. What you could do is one of two things:
    1)
    Code:
    char * retvar()
    {
         char retvar[] = "retvar";
         return retvar;
    }
    
    int main()
    {
         cout << retvar() << endl;
         return 0;
    }
    OR

    2)
    Code:
    char * retvar()
    {
         char retvar[] = "retvar";
         return retvar;
    }
    
    int main()
    {
         char * retvar = retvar();
         cout << retvar << end;
         return 0;
    }
    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  6. #6
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    You can do this a bunch of ways, here's one more.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char *retvar()
    {
    	char *ret = new char[sizeof("retvar")];
    	return strcpy( ret, "retvar" );
    }
    
    int main()
    {
    	char *string = retvar();
    	
    	cout<< string <<endl;
    	delete string;
    
    	return 0;
    }
    And another that's closer to what you posted but not as handy as other ways.
    Code:
    #include <iostream>
    using namespace std;
    
    char *retvar()
    {
    	return "retvar";
    }
    
    int main()
    {
    	cout<< retvar() <<endl;
    	return 0;
    }
    And if you want to return multiple values it's best to either use pointers for your parameters or pass a struct with all of the variables you want to return.

    harryP, neither of your suggestions will work because both still try to return a local variable, this variable is destroyed when the function returns and main ends up with garbage. You also can't use the same name retvar for everything or the compiler will panic since it has no idea what you want when you say retvar.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Oops, I was trying to keep it a little like his example whilst getting it to work - guess I'll just give a wroking answer next time
    Couldn't think of anything interesting, cool or funny - sorry.

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. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM