C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 1st, 2002, 05:31 PM
irm_lexx's Avatar
irm_lexx irm_lexx is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 231 irm_lexx User rank is Private First Class (20 - 50 Reputation Level)irm_lexx User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 15 h 47 sec
Reputation Power: 7
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?

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!

Reply With Quote
  #2  
Old April 1st, 2002, 05:37 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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?

Reply With Quote
  #3  
Old April 1st, 2002, 06:34 PM
irm_lexx's Avatar
irm_lexx irm_lexx is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 231 irm_lexx User rank is Private First Class (20 - 50 Reputation Level)irm_lexx User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 15 h 47 sec
Reputation Power: 7
Ok, I use Borland C++. and I need to return a string through function return, how can I do that?

Reply With Quote
  #4  
Old April 2nd, 2002, 03:29 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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.

Reply With Quote
  #5  
Old April 2nd, 2002, 09:49 AM
CodeBastard CodeBastard is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Sweden
Posts: 0 CodeBastard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to CodeBastard
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. :-)

Reply With Quote
  #6  
Old April 2nd, 2002, 07:03 PM
irm_lexx's Avatar
irm_lexx irm_lexx is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 231 irm_lexx User rank is Private First Class (20 - 50 Reputation Level)irm_lexx User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 15 h 47 sec
Reputation Power: 7
Thanks that works,
but can you explain int main(int argc, char **argv) { , thanks

Reply With Quote
  #7  
Old April 2nd, 2002, 10:58 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Evil Strings.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway