Thread: Evil Strings.

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13

    Angry Evil Strings.


    Why does it have to be so complecated with strings?
    How do you return a string through a return function?
    why do you have to use that strcpy(); thing? Why cant you compare two strings with an equal sing?
    My question is, how do you return a string through a return function?:confused:

    i have a function that does something and then returns a string.
    so if you have a function that returns something it should be like.

    string = function(); but you cant with strings. Please help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    There are literally hundreds of string classes out there. If you're using MSVC++, you have things from the LPCTSTR, to the ever-useful CString. These can be returned from functions. Other environments will have similar classes, or you can find tons of user created ones.

    The problem arises with memory management. How do you pass around and make copies of variables that have no type-defined length? Its for the same reason you can't return arrays. You can return an int, for example, because an int is always a fixed byte size. A string though, varies, and may have its data fragmented across different memory locations.

    You need to define a class that worries about that for you. One that internally keeps track of the size, and location of all of its data. One that has appropriate copy constructors, that over-rides operator =, or operator ==, etc. But why bother defining a class like that when there are so many good ones out there?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    Ok, I use Borland C++. and I need to return a string through function return, how can I do that?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    I don't have access to Borland C++, so I don't know what may come stock. Check your documentation, there has to be something. Also, try here:

    http://www.codeguru.com/string/index.shtml

    I haven't looked at any of them, but its a start.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Sweden
    Posts
    0
    Rep Power
    0
    You can always use the standard template library's (STL) string.
    Here is a simple example:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string foostring() {
        string mystring = "My string";
        return mystring;
    }
    
    int main(int argc, char **argv) {
        string foo = foostring();
        cout << "Foo: " << foo<< endl;
        return 0;
    }
    It's alot easier than handling arrays of chars. :-)
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    Thanks that works,
    but can you explain int main(int argc, char **argv) { , thanks
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    That's how you process command line messages into your app. argc is Argument Count, and argv is Argument Vector. The char** means that the parameter is a pointer to a pointer to a char. Or, you can look at it as a pointer to an array of strings, which is easier to wrap your head around. Remember that an array name is just a pointer to the first array element. So why can't you declare the char** argv as a char* argv[]? You can. You can even declare it as a char argv[][]. They're all equal. Try this program out:

    Code:
    #include <iostream>
    int main(int argc, char** argv)
    {
        std::cout << "Got " << argc << " arguments." << std::endl;
        for (int i = 0; i < argc; i++)
            std::cout << "Param " << i << ": " << argv[i] << std::endl;
        return 0;
    }
    Run it from the comand line like this:

    Code:
    c:\>foo.exe
    
    // Generates an output of:
    Got 1 arguments.
    Param 1: foo.exe
    
    c:\>foo.exe Hello world!
    
    // Generates:
    Got 3 arguments.
    Param 1: foo.exe
    Param 2: Hello
    Param 3: world!

IMN logo majestic logo threadwatch logo seochat tools logo