|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
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? 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
|
|||
|
|||
|
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? |
|
#3
|
||||
|
||||
|
Ok, I use Borland C++. and I need to return a string through function return, how can I do that?
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. :-) |
|
#6
|
||||
|
||||
|
Thanks that works,
but can you explain int main(int argc, char **argv) { , thanks |
|
#7
|
|||
|
|||
|
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! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Evil Strings. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|