string literals

Discussion in 'C++' started by Kevin Buffardi, Oct 20, 2003.

  1. 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
     
    Kevin Buffardi, Oct 20, 2003
    #1
    1. Advertising

  2. Kevin Buffardi

    Mike Wahler Guest

    "Kevin Buffardi" <> wrote in message
    news:bmvo37$pbu6d$-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/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
     
    Mike Wahler, Oct 20, 2003
    #2
    1. Advertising

  3. Kevin Buffardi

    David White Guest

    Kevin Buffardi <> wrote in message
    news:bmvo37$pbu6d$-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
     
    David White, Oct 20, 2003
    #3
  4. > 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
     
    Kevin Buffardi, Oct 20, 2003
    #4
  5. > 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
     
    Kevin Buffardi, Oct 20, 2003
    #5
  6. Kevin Buffardi

    Gregg Guest

    "Kevin Buffardi" <> wrote in
    news:bmvt8u$rcvus$-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
     
    Gregg, Oct 20, 2003
    #6
  7. Kevin Buffardi

    David White Guest

    Kevin Buffardi <> wrote in message
    news:bmvtq0$r95es$-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
     
    David White, Oct 20, 2003
    #7
  8. > > 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
     
    Peter van Merkerk, Oct 20, 2003
    #8
  9. Kevin Buffardi

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Oct 20, 2003
    #9
  10. Kevin Buffardi

    Ron Natalie Guest

    "Kevin Buffardi" <> wrote in message news:bmvt8u$rcvus$-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.
     
    Ron Natalie, Oct 20, 2003
    #10
  11. > 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.
     
    Andrew Koenig, Oct 20, 2003
    #11
  12. > 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
     
    Kevin Buffardi, Oct 22, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harri Pesonen

    String literals in Java

    Harri Pesonen, May 28, 2004, in forum: Java
    Replies:
    59
    Views:
    15,147
    Jim Cochrane
    Jun 2, 2004
  2. Pete Elmgreen

    character literals and string

    Pete Elmgreen, Nov 24, 2004, in forum: Java
    Replies:
    3
    Views:
    4,761
  3. Purush
    Replies:
    4
    Views:
    1,775
    Purush Rudrakshala
    Apr 13, 2005
  4. Replies:
    4
    Views:
    904
    Roedy Green
    Nov 21, 2005
  5. John Goche
    Replies:
    8
    Views:
    16,581
Loading...

Share This Page