【问题标题】:Why after using strcat function original string changed?为什么使用strcat函数后原来的字符串变了?
【发布时间】:2017-06-07 09:51:52
【问题描述】:

我有两个字符数组。当我尝试使用 strcat 函数连接两个字符串时。 然后我的字符串“a”长度从 9 减少到 6。 我也丢失了我的字符串“a”。字符串 b 也发生了变化。请参阅输出。为什么会这样???

这就是我所做的

#include <bits/stdc++.h>

using namespace std;
int main() {
    char a[]="roomies!!";
    char b[]="hey kammo DJ ";
    char *c;
    c=new char[50];
    cout<<"before:-\n";
    cout<<"len of a is "<<strlen(a)<<'\n';
    cout<<"len of b is "<<strlen(b)<<'\n';
    cout<<"len of c is "<<strlen(c)<<'\n';
    cout<<"string a is = "<<a<<'\n';
    cout<<"string b is = "<<b<<'\n';
    cout<<"string c is = "<<c<<'\n';
    c=strcat(b,a);
    cout<<"\nafter:-\n";
    cout<<"len of a is "<<strlen(a)<<'\n';
    cout<<"len of b is "<<strlen(b)<<'\n';
    cout<<"len of c is "<<strlen(c)<<'\n';
    cout<<"string a is = "<<a<<'\n';
    cout<<"string b is = "<<b<<'\n';
    cout<<"string c is = "<<c<<'\n';
return 0;
}

输出:-

before:-
len of a is 9
len of b is 13
len of c is 3
string a is = roomies!!
string b is = hey kammo DJ
string c is = =

after:-
len of a is 6
len of b is 22
len of c is 22
string a is = mies!!
string b is = hey kammo DJ roomies!!
string c is = hey kammo DJ roomies!!

【问题讨论】:

  • 因为,呃,strcat() 就是这么做的?您是否考虑过查阅文档?而不是猜测?
  • @EJP 那么为什么 char 数组 "a" 改变了? char arrya "b" 只能更改。
  • 因为您的b 泛滥并从事未定义的行为。
  • 它的 C++。使用 std::string!。

标签: c++ strcat


【解决方案1】:

根据strcat spec

"如果目标数组不大,则行为未定义 足够 src 和 dest 的内容以及终止的 null 字符。”

你的目标数组是“b”,它显然不足以存储“a”和“b”的内容,所以你得到了一个未定义的行为,导致修改了“a”字符串。

【讨论】:

    【解决方案2】:

    strcat() 将源字符串附加到目标字符串并返回目标字符串。

    char * strcat ( char * destination, const char * source ); 
    

    所以你的陈述

    c = strcat(b,a) 
    

    所以数组 b 和 c 将具有相同的值。 strcat()

    编辑: "a" 改变了,因为你溢出了 "b" 数组。由于它的 c++,您可以只使用 std::string 而不是字符数组。

    std::string a = "hi" ; 
    std::string b = "this is concat" ;
    std::string c = a + b ;
    

    【讨论】:

    • 知道了。但我最初的问题是为什么字符串“a”发生了变化。在使用 strcat 函数时。
    • @RakeshSharma 你没明白... strcat 不返回新的字符数组。 b 已更改(通过附加 a)。
    • @deviantfan 为什么“字符串“a”被改变了..!
    • @RakeshSharma 您知道,在回答后更改您的问题对任何人都没有帮助。 ...答案:UB。您的代码有多次。
    • @RakeshSharma "b" 已更改,因为那是您的目标数组。如果您的问题是为什么“a”会发生变化,那可能是因为数组“b”不足以容纳串联的字符串。在这种情况下,行为是未定义的。
    【解决方案3】:

    strcat on C++ reference

    将源字符串的副本附加到目标字符串。目标中的终止空字符被第一个字符覆盖 源字符,并且在末尾包含一个空字符 由destination中两者连接形成的新字符串。

    您可以使用std::string 来获得所需的结果。

    【讨论】:

      【解决方案4】:

      函数strcat 具有签名char *strcat( char *dest, const char *src ) 并将字符串src 的内容附加到dest 指向的字符串的末尾,即它改变 内存的内容dest 指向的。这要求dest 指向的内存足够大以容纳srcdest 这两个字符串。

      因此,您的调用 strcat(b,a) 实际上会产生未定义的行为,因为 b 表示的内存块能够容纳 14 个字节(即 "hey kammo DJ "+ 1 的长度),但不能容纳任何额外的字符串。

      所以你宁愿写这样的东西:

      strcpy (c,b);
      strcat (c,a);
      

      或:

      snprintf(c, 50, "%s%s", b, a);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多