string literals

K

Kevin Buffardi

To those experts in standard C/C++...

In past I've been quick to use the string.h library for convenience, but now
I'm working on a project where I'm trying to maximize portability so I want
to conform to c++ standards as much as I can. Can someone point me in the
right direction for using character arrays (I assume is the most convenient
way) specifically with assigning values and passing/returning from
functions.

I appreciate it. It's been a while since I've tried manipulating strings
without string.h and the only C++ book I have with me is specific to Borland
compilers and not standard C++. Thanks.
 
M

Mike Wahler

Kevin Buffardi said:
To those experts in standard C/C++...

In past I've been quick to use the string.h library for convenience, but now
I'm working on a project where I'm trying to maximize portability so I want
to conform to c++ standards as much as I can.
Can someone point me in the
right direction for using character arrays (I assume is the most convenient
way)

IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.
specifically with assigning values and passing/returning from
functions.

The following (contrived) example demonstrates
passing strings (by reference) to a function,
and returning a string from a function:

#include <iostream>
#include <string>

std::string func(const std::string& arg1, const std::string& arg2)
{
return arg1 + ' ' + arg2;
}

int main()
{
std::string s1("Hello");
std::string s2("world");
std::cout << func(s1, s2) << '\n'; /* prints "Hello world" */
return 0;
}
I appreciate it. It's been a while since I've tried manipulating strings
without string.h and the only C++ book I have with me is specific to Borland
compilers and not standard C++.

http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c__.htm
http://www.accu.org/bookreviews/public/reviews/0sb/advanced_c__.htm

http://www.parashift.com/c++-faq-lite/
http://www.contrib.andrew.cmu.edu/~ajo/faqs/acllc-c++.html#q6.3

-Mike
 
D

David White

Kevin Buffardi said:
To those experts in standard C/C++...

Standard C++ is all that matters here. C and C++ are different languages and
there's no such language as C/C++.
In past I've been quick to use the string.h library for convenience, but now
I'm working on a project where I'm trying to maximize portability so I want
to conform to c++ standards as much as I can. Can someone point me in the
right direction for using character arrays (I assume is the most convenient
way) specifically with assigning values and passing/returning from
functions.

Surmising from your subject line, do you mean string literals only, or other
character arrays as well? String literals are pretty limited, since the
strings' contents have to known at compile time and can't be modified. Other
character arrays are definitely not the most convenient way of assigning
values and returning strings from functions.

I suggest that you use std::string objects (use the <string> header file)
for assigning string contents and returning strings from functions. For
passing to functions that don't modify the string contents, you can also use
std::strings, but const char* is often sufficient.

DW
 
K

Kevin Buffardi

IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.

That's standard? I had been using the string type, but I was under the
impression that it was not standard because the g++ compiler I was using
threw a fit when I tride to #include<string> .
 
K

Kevin Buffardi

Standard C++ is all that matters here. C and C++ are different languages
and
there's no such language as C/C++.

Yes I know "C/C++" isn't a language, but I didn't know if there was a C
standard for string manipulation that was brought over to C++. In fact, my
code should also compile on a C-compiler, so either would work... hence my
original "C/C++" notation.
Surmising from your subject line, do you mean string literals only, or other
character arrays as well? String literals are pretty limited, since the
strings' contents have to known at compile time and can't be modified.

You're right, the subject line is deceiving. I need to manipulate the
strings during run-time. I'm looking for a _portable_ solution that should
work with any C++ compiler on any platform. Given choices, I'd go for the
most convenient of them if they all satisfy the portability requirement.

However, if I was wrong and <string> is standard, then I'll just stay with
it, as it does what I need it to do and I've been using it for years now so
I'd consider it convenient.
 
G

Gregg

That's standard? I had been using the string type, but I was under
the impression that it was not standard because the g++ compiler I was
using threw a fit when I tride to #include<string> .

Yes, it's standard. You would know that if you had current reference
material. Set aside most C++ books written before 1998. Get a copy of

- The C++ Programming Language, 3rd edition, Stroustrup.
- The C++ Standard Library: A Tutorial and Reference, Nicolai Josuttis.

I highly recommend the latter if you want to learn about the standard
library, of which std::string is a part. In addition, if you want an
introduction to C++ for someone with programming experience, get a copy
of

- Accelerated C++, Andrew Koenig, Barbara Moo.

If your compiler does not accept

#include <string>

then it is either old (in the case of gcc, pre-3.0), not installed
correctly, or not being invoked correctly. Do

gcc --version

to see what version you have.

Gregg
 
D

David White

Kevin Buffardi said:
However, if I was wrong and <string> is standard, then I'll just stay with
it, as it does what I need it to do and I've been using it for years now so
I'd consider it convenient.

Yep, the std::string type is definitely standard and portable.

DW
 
P

Peter van Merkerk

Standard C++ is all that matters here. C and C++ are different
languages
and

Yes I know "C/C++" isn't a language, but I didn't know if there was a C
standard for string manipulation that was brought over to C++. In fact, my
code should also compile on a C-compiler, so either would work... hence my
original "C/C++" notation.

If the code should also compile on a C compiler you will have to do
without to all the benefits of C++, including the std::string class. The
vast majority of C code compiles also on C++ compilers (unless C99
extensions are used), including string manipulation. If the code really
should compile on a C compiler, it is better to start with a C compiler
first, and test later if it also compiles on a C++ compiler.
You're right, the subject line is deceiving. I need to manipulate the
strings during run-time. I'm looking for a _portable_ solution that should
work with any C++ compiler on any platform. Given choices, I'd go for the
most convenient of them if they all satisfy the portability
requirement.

If you are willing to say goodbye to C compatibility, std::string meets
your requirements perfectly.
 
R

Rolf Magnus

Gregg said:
If your compiler does not accept

#include <string>

then it is either old (in the case of gcc, pre-3.0),

Actually, even gcc 2.91.66 (the oldest one I have - from early '99)
already has that header.
 
R

Ron Natalie

Kevin Buffardi said:
That's standard? I had been using the string type, but I was under the
impression that it was not standard because the g++ compiler I was using
threw a fit when I tride to #include<string> .
What kind of fit? Did you remember it's in namespace std? I use string with
G++ all the time.
 
A

Andrew Koenig

However said:
it, as it does what I need it to do and I've been using it for years now so
I'd consider it convenient.

<string> is standard. Note, however, that <string.h> is something
completely different--it's part of the C standard library, and declares
functions such as strcpy.
 
K

Kevin Buffardi

Did you remember it's in namespace std?

Ding ding ding.... we have a winner. Guessing it's been 3 years since I've
used string on g++ and completely forgot that it required that one little
line. Silly me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top