473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

string or const char * in a function parameter list ?

iu2
Hi all,

I find myselft wondering about the recommended way to pass strings to
functions:

int func(string s);

or

int func(const char *s);

I usually pass a const char * since it's shorter referring to s
instead of s.c_str() inside 'func'. In case some string manipulation
need arises, I can always launch a string or a stringstrem insinde
'func'.
Is there a good reason to pass a 'string' instead of a const char * ?

thanks
iu2

Aug 23 '07 #1
3 15661
On Aug 23, 12:08 pm, iu2 <isra...@elbit.co.ilwrote:
Hi all,

I find myselft wondering about the recommended way to pass strings to
functions:

int func(string s);

or

int func(const char *s);

I usually pass a const char * since it's shorter referring to s
instead of s.c_str() inside 'func'. In case some string manipulation
need arises, I can always launch a string or a stringstrem insinde
'func'.
Is there a good reason to pass a 'string' instead of a const char * ?

Using string class is much much much safer than using bare character
pointers.
http://www.parashift.com/c++-faq-lit....html#faq-34.1
http://www.parashift.com/c++-faq-lit....html#faq-17.5

-N

Aug 23 '07 #2
First of all, you should not pass a string object by value most of the
time. If your function is not going to modify the string then you
should pass a const string& to avoid a copy of the object being made
(if you are passing a const char* as in your other example it means
that you are definitely not going to modify the string).

That said, if you have to ask the question then you should be using a
string of some sort instead of a const char*. It's much safer this
way.

Of course, if the program is really simple or you have a need for
speed, then char* can be a better choice.

Aug 23 '07 #3
On Aug 23, 8:08 am, iu2 <isra...@elbit.co.ilwrote:
Hi all,

I find myselft wondering about the recommended way to pass strings to
functions:

int func(string s);
or
int func(const char *s);
Firstly, do not use the first form: prefer (const string&).
With that fix in place, I would say it depends on a number of factors:
1. If func needs the facilities of std::string, provide the const
string& form
2. If you envisage that a lot of calling code will use C-style
strings, provide
the const char* form.

Finally, it's easy enough to provide both and delegate; where you put
the
'meat' of the processing depends on what func actually does: Either:

int func (const string& s) { .... }
int func (const char* s) { return func(string(s)); }

Or

int func (const string& s) { return func(s.c_str()); }
int func (const char* s) { ... }

Cheers.

Aug 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
6
by: Sona | last post by:
Hi, What's the advantage/disadvantage of using a "const char*" over a "char*" ? I read some place that char* are string literals that on some machines are stored in a read only memory and cannot...
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
5
by: sean345 | last post by:
While compiling my program I get the following g++ error: error: no matching function for call to `ErrorLog::file_error(std::string&, std::string&, const char)' note: candidates are: void...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
34
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
3
by: Rick Helmus | last post by:
Hello all In a few classes I have overloaded functions for C style strings and STL strings like this class SomeClass { void f(const char *s); void f(const std::string &s); };
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.