473,698 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15686
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
2571
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 information." I put a cout statement (//cout << "len of cPtr: " << _len << endl; ) in my constructor and see that _len is 3435973837 though my character pointer has only a few characters. Then I get the usual message as state above.
6
4755
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 be modified... does that apply on const char*? thanks Sona
15
2232
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
16816
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 *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
5
5420
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 ErrorLog::file_error(const std::string&, int, const std::string&) The error line is: log.file_error(filename, line, "variable is not assigned a value");
2
5035
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 because in further editions of .net this will be deprecated or so on.. . so, please, anybody can tellme how to convert from System::String::^ to char* and viceversa?
34
31295
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
3
4333
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
9524
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++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.