Thread: C++ String: How to assign or compare strings?

C++ String: How to assign or compare strings?

  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    C++ String: How to assign or compare strings?

    Q: How to assign or compare strings?

    A: Assignment and comparison are not necessarily related, but we decided to handle them together because the mistakes made by programmers beginning with C++ have a common root, that is, the tendence to use a 'natural' syntax.

    Assignment

    This actually is an issue only when working with C-style strings, i.e. 'char*'. The most common mistake is this:

    Code:
    char *s1, *s2;
    //...
    s2 = s1; // might not do what one would think
    This statement assigns to the char pointer 's2' the same vale as 's1'. With other words, 's1' and 's2' will point to one and the same address in memory. By no means is the string 's1' copied into 's2'.

    Another mistake that is frequently made by beginners is something like this:

    Code:
    char* s = "Hello";
    s[1] = 'a';             // attempting to change the 'e' to an 'a'
    //...
    Chances are to get an access violation when running this code. The reason is that, again, the assignment applies to the char pointer 's', i.e. it will point to the address of the constant string literal "Hello'. Since the compiler is allowed to place such const literals in read-only memory, attempting to modify it has undefined results.

    The correct solution is to use the 'strcpy()' function or one of its relatives.

    Code:
    char *s;
    s = new char[strlen("Hello") + 1];  // reserve one byte for the trailing '\0'
    strcpy(s, "Hello");
    //...
    delete[] s;
    Both 'CString' and the Standard C++ class 'std::string' are user-friendly and take care by themselves of this details. This code is perfectly ok:

    Code:
    #include <string>
    
    std::string s;
    s = "Hello";
    s[1] = 'a';
    Comparison

    This also is an issue only when working with C-style strings, i.e. 'char*'. The most common mistake is this:
    Code:
    char *s1, *s2;
    
    if(s1 == s2)       // wrong!!!!
    This statement may seem to be the natural syntax, but it unfortunately compares the value of two char pointers and by no means the strings they point to. To compare C-style strings you have to use the 'strcmp()' function or one of its relatives:

    Code:
    char *s1, *s2;
    
    if(!strcmp(s1, s2))
    Note, that 'strcmp()' returns 0 if the strings are identical, thus, the ' !strcmp(...)' in the if statement. Have a look in e.g. MSDN for further details on 'strcmp()' and other string comparison functions.

    Both 'CString' and the Standard C++ class 'std::string' have overloaded comparison operators, which leads to a more natural syntax:

    Code:
    #include <string>
    
    std::string s1, s2;
    if(s1 == s2)
    All this being said, it should be clear why you always should prefere working with a string wrapper class over working with C-style strings. And there is much more std::string can do for you than this!


    Last edited by Andreas Masur; July 24th, 2005 at 07:32 AM.
posted on 2013-01-14 00:41  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2013/01/14/2859094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值