将整数转换为c ++中的字符指针,然后将其附加到另一个字符指针

时间:2010-12-31 11:13:59

标签: c++ pointers

我有以下代码将整数(一个分数)转换为角色,然后用玩家的名字(player1)附加它。之后会显示出来。它是更大项目的一部分:

    #include <iostream>
#include <string.h>
using namespace std;

char* convertIntTochar(int number)
{
    char t[3];
    t[0] = 0;
    t[1] = 0;
    t[2] = '\0';

    int i = 0;
    for(; number != 0; i++)
    {
        t[i] = ((number%10) + 48);
        number/=10;
    }

    if(i == 2)
    {
        char temp = t[0];
        t[0] = t[1];
        t[1] = temp; 
    }
    else
        t[i] = '\0';
    char *ans = t;
    return ans;
}

int main()
{
    char str11[] = "Player1: ";
    char *str1 = str11;
    char *str2 = convertIntTochar(11);
    strcat(str1 , str2);

    while(*str1)
    {
        cout<<*(str1++);
    }

    return 0;
}

它正确编译但是当我运行它时,它显示以下错误:

*** stack smashing detected ***: ./a.out terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x9b3390]
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x9b333a]
./a.out[0x80487ff]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x8e7bd6]
./a.out[0x8048621]
======= Memory map: ========
00110000-00134000 r-xp 00000000 08:06 2887608    /lib/tls/i686/cmov/libm-2.11.1.so
00134000-00135000 r--p 00023000 08:06 2887608    /lib/tls/i686/cmov/libm-2.11.1.so
00135000-00136000 rw-p 00024000 08:06 2887608    /lib/tls/i686/cmov/libm-2.11.1.so
004b9000-004d4000 r-xp 00000000 08:06 2887597    /lib/ld-2.11.1.so
004d4000-004d5000 r--p 0001a000 08:06 2887597    /lib/ld-2.11.1.so
004d5000-004d6000 rw-p 0001b000 08:06 2887597    /lib/ld-2.11.1.so
0077d000-00866000 r-xp 00000000 08:06 2756275    /usr/lib/libstdc++.so.6.0.13
00866000-00867000 ---p 000e9000 08:06 2756275    /usr/lib/libstdc++.so.6.0.13
00867000-0086b000 r--p 000e9000 08:06 2756275    /usr/lib/libstdc++.so.6.0.13
0086b000-0086c000 rw-p 000ed000 08:06 2756275    /usr/lib/libstdc++.so.6.0.13
0086c000-00873000 rw-p 00000000 00:00 0 
008d1000-00a24000 r-xp 00000000 08:06 2887604    /lib/tls/i686/cmov/libc-2.11.1.so
00a24000-00a25000 ---p 00153000 08:06 2887604    /lib/tls/i686/cmov/libc-2.11.1.so
00a25000-00a27000 r--p 00153000 08:06 2887604    /lib/tls/i686/cmov/libc-2.11.1.so
00a27000-00a28000 rw-p 00155000 08:06 2887604    /lib/tls/i686/cmov/libc-2.11.1.so
00a28000-00a2b000 rw-p 00000000 00:00 0 
00a3b000-00a58000 r-xp 00000000 08:06 2883667    /lib/libgcc_s.so.1
00a58000-00a59000 r--p 0001c000 08:06 2883667    /lib/libgcc_s.so.1
00a59000-00a5a000 rw-p 0001d000 08:06 2883667    /lib/libgcc_s.so.1
00b74000-00b75000 r-xp 00000000 00:00 0          [vdso]
08048000-08049000 r-xp 00000000 08:06 4719693    /home/dhruv/Desktop/a.out
08049000-0804a000 r--p 00000000 08:06 4719693    /home/dhruv/Desktop/a.out
0804a000-0804b000 rw-p 00001000 08:06 4719693    /home/dhruv/Desktop/a.out
08b67000-08b88000 rw-p 00000000 00:00 0          [heap]
b77f7000-b77f9000 rw-p 00000000 00:00 0 
b780d000-b7810000 rw-p 00000000 00:00 0 
bfd2a000-bfd3f000 rw-p 00000000 00:00 0          [stack]
Player1: "�ӿ�XMAborted

这是什么原因?怎么能纠正。我已将null终止字符放在convertIntTochar函数中。

4 个答案:

答案 0 :(得分:6)

这里有很多问题......

  1. convertIntTochar仅适用于2位数字。没有检查。
  2. convertIntTochar中定义的
  3. char t[3]是一个局部变量,你不能返回指向它的指针并在convertIntTochar外面使用这个指针。
  4. strcat(str1 , str2);附加到已满的数组(str11),以便覆盖堆栈。
  5. 只需切换到std :: strings,它就会更简单。

答案 1 :(得分:2)

   char str11[] = "Player1: ";

这就是问题所在。没有足够的空间进行字符串连接。试试这个:

   char str11[100] = "Player1: ";

更好的是,使用std::string代替类似C char*。解决字符串问题的最小可能更改是这些(因为存在using namespace std然后std::中的std::string部分可以省略,但我只想将其保留在其中:

#include <iostream>
#include <string> // instead of <string.h>
using namespace std;

std::string convertIntTochar(int number)
{
    ...
}

int main()
{
    std::string str1 = "Player1: ";
    std::string str2 = convertIntTochar(11);
    str1 += str2;

    cout << str1;

    // Or even more effective, just one line of code:
    cout << "Player1: " << convertIntTochar(11);

    return 0;
}

答案 2 :(得分:1)

使用std :: strings和std :: ostringstreams,它更简单。

#include <sstream>
#include <iostream>
std::ostringstream player_score_stream;
player_score_stream << "Player1: " << score_as_an_integer;
std::string player_score(player_score_stream.str());
std::cout << player_score;

如果你想要一个只读的C字符串,请使用player_score.c_str(),它返回一个const char *

答案 3 :(得分:0)

将int转换为char *,看看是否可以在编译器中使用itoa 如果不支持,你可以找到它的实现来做你想做的事 那就是你必须使用C-strings

来做
相关问题