473,385 Members | 1,341 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,385 software developers and data experts.

string literals

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.

--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
Jul 19 '05 #1
11 2412

"Kevin Buffardi" <kb******@mwc.edu> wrote in message
news:bm************@ID-80902.news.uni-berlin.de...
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/publ...nner_s_c__.htm
http://www.accu.org/bookreviews/publ...vanced_c__.htm

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

-Mike
Jul 19 '05 #2
Kevin Buffardi <kb******@mwc.edu> wrote in message
news:bm************@ID-80902.news.uni-berlin.de...
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

Jul 19 '05 #3
> 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> .

--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
Jul 19 '05 #4
> 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.

--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
Jul 19 '05 #5
"Kevin Buffardi" <kb******@mwc.edu> wrote in
news:bm************@ID-80902.news.uni-berlin.de:
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> .


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
Jul 19 '05 #6
Kevin Buffardi <kb******@mwc.edu> wrote in message
news:bm************@ID-80902.news.uni-berlin.de...
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

Jul 19 '05 #7
> > 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.
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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #8
Gregg wrote:
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.

Jul 19 '05 #9

"Kevin Buffardi" <kb******@mwc.edu> wrote in message news:bm************@ID-80902.news.uni-berlin.de...
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> .

What kind of fit? Did you remember it's in namespace std? I use string with
G++ all the time.
Jul 19 '05 #10
> 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.


<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.
Jul 19 '05 #11
> 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.
--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
Jul 19 '05 #12

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

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
8
by: junky_fellow | last post by:
what would be the output for the following piece of code ? if ( "hello" == "hello" ) printf("True\n"); else printf("False\n"); What is the reason for that ?
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
8
by: arnuld | last post by:
i tried to output these 2 to the std. output: const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; const std::string exclam = "!"; const std::string...
4
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello all, I'm learning C and I still am struggling to understand some basic concepts. For example, I read in the standard that with functions such as strcpy, 'If copying takes place between...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.