Thread: question about returning an array

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    question about returning an array

    i would like to write a function to return a array....
    should i write in this way?

    Code:
    char get_local_ftp_Sdir()
    	{
    		char local_ftp_Sdir[256] = "webRfq\\ftp";
    		return *local_ftp_Sdir;
    	}

    if it is correct, would you please explain what does it mean by
    return *local_ftp_dir??

    thank you.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That is not correct. That would return the first character in the array. Your return type is "char" so a single character is all that can be returned. You need to either pass an array as a parameter and fill it in or return a char* like so

    Code:
    #include <iostream>
    using namespace std;
    
    char* Print( void )
    {
      static char s[] = "Hello World";
      return s;
    }
    
    int main( void )
    {
      cout << Print( ) << endl;
      return 0;
    }
    Note that you make the array static so the variable is placed on the heap and not on the stack. If it was on the stack it would be destroyed at the end of the function Print's scope. We are ok if it is on the heap.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you

    thank you for teaching me ^_^

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You are much better off using either std::string (for strings) or std::vector (for arrays of any other type). In fact, it would be a good idea to replace ANY dynamically allocated array with a std::vector, and it wouldn't hurt to replace all arrays in general.

    See http://cboard.cprogramming.com/showt...threadid=43075 for more discussion of this.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Correct me if I'm wrong, but doesn't that return a pointer to a portion of the stack that theoretically can be overwritten later?? Since that array has not been created on the heap doesn't it reside on the local stack of the function at negative offsets to EBP? Since C does a mov esp,ebp and yet is returning a pointer to the stack portion above EBP won't this cause problems later?

    Not to stray off topic, but Just curious

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Bubba
    Correct me if I'm wrong, but doesn't that return a pointer to a portion of the stack that theoretically can be overwritten later?? Since that array has not been created on the heap doesn't it reside on the local stack of the function at negative offsets to EBP? Since C does a mov esp,ebp and yet is returning a pointer to the stack portion above EBP won't this cause problems later?

    Not to stray off topic, but Just curious
    I'm not completely sure on this, but I think there is memory for the heap, local variables, and global variables.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Sorry all - did not read Mr. Wizard's post carefully enough. Sometimes I just scroll through these things too fast.



    I failed to notice that he made the array static. Doh!!!

    :konk:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM