如何在c++中打印字符数组

时间:2021-04-17 22:52:52

how can i print a char array such i initialize and then concatenate to another char array? Please see code below

如何打印一个char数组,这样我将初始化并连接到另一个char数组?请看下面的代码

int main () {
char dest[1020];
char source[7]="baby";
cout <<"source: " <<source <<endl;
cout <<"return value: "<<strcat(dest, source) <<endl;
cout << "pointer pass: "<<dest <<endl;
return 0;
}

this is the output

这是输出

source: baby
return value: v����baby
pointer pass: v����baby

basically i would like to see the output print

基本上,我想看看输出结果

source: baby
return value: baby
pointer pass: baby

4 个解决方案

#1


9  

You haven't initialized dest

你还没有初始化的桌子

char dest[1020] = ""; //should fix it

You were just lucky that it so happened that the 6th (random) value in dest was 0. If it was the 1000th character, your return value would be much longer. If it were greater than 1024 then you'd get undefined behavior.

幸运的是,在dest中,第6(随机)值恰好是0。如果是第1000个字符,那么返回值就会长得多。如果它大于1024,你会得到未定义的行为。

Strings as char arrays must be delimited with 0. Otherwise there's no telling where they end. You could alternatively say that the string ends at its zeroth character by explicitly setting it to 0;

字符串作为字符数组必须用0分隔。否则就无从知晓它们的结局。您也可以通过显式地将字符串设置为0来表示该字符串在其第零字符处结束;

char dest[1020];
dest[0] = 0;

Or you could initialize your whole array with 0's

或者你可以用0初始化整个数组

char dest[1024] = {};

And since your question is tagged C++ I cannot but note that in C++ we use std::strings which save you from a lot of headache. Operator + can be used to concatenate two std::strings

由于您的问题被标记为c++,我不得不注意,在c++中,我们使用std::string,它使您避免了很多麻烦。操作符+可用于连接两个std::string

#2


4  

Don't use char[]. If you write:

不要使用char[]。如果你写:

std::string dest;
std::string source( "baby" )
//  ...
dest += source;

, you'll have no problems. (In fact, your problem is due to the fact that strcat requires a '\0' terminated string as its first argument, and you're giving it random data. Which is undefined behavior.)

你不会有问题的。(实际上,您的问题是由于strcat需要一个'\0'终止字符串作为它的第一个参数,并且您给它随机的数据。未定义的行为。)

#3


1  

your dest array isn't initialized. so strcat tries to append source to the end of dest wich is determined by a trailing '\0' character, but it's undefined where an uninitialized array might end... (if it does at all...)

你的数组没有初始化。所以strcat尝试将源代码追加到最重要的位置,这是由一个尾随的“\0”字符决定的,但是它没有定义一个未初始化的数组可能会结束……(如果有的话……)

so you end up printing more or less random characters until accidentially a '\0' character occurs...

所以你最终会打印出或多或少的随机字符,直到偶然地出现一个“\0”字符……

#4


0  

Try this

试试这个

#include <iostream>

using namespace std;

int main()
{
    char dest[1020];
    memset (dest, 0, sizeof(dest));
    char source[7] = "baby";
    cout << "Source: " << source << endl;
    cout << "return value: " << strcat_s(dest, source) << endl;
    cout << "pointer pass: " << dest << endl;
    getchar();
    return 0;
}

Did using VS 2010 Express. clear memory using memset as soon as you declare dest, it's more secure. Also if you are using VC++, use strcat_s() instead of strcat().

使用VS 2010 Express。一旦声明了dest,使用memset清除内存,更安全。如果您正在使用vc++,那么使用strcat_s()而不是strcat()。

#1


9  

You haven't initialized dest

你还没有初始化的桌子

char dest[1020] = ""; //should fix it

You were just lucky that it so happened that the 6th (random) value in dest was 0. If it was the 1000th character, your return value would be much longer. If it were greater than 1024 then you'd get undefined behavior.

幸运的是,在dest中,第6(随机)值恰好是0。如果是第1000个字符,那么返回值就会长得多。如果它大于1024,你会得到未定义的行为。

Strings as char arrays must be delimited with 0. Otherwise there's no telling where they end. You could alternatively say that the string ends at its zeroth character by explicitly setting it to 0;

字符串作为字符数组必须用0分隔。否则就无从知晓它们的结局。您也可以通过显式地将字符串设置为0来表示该字符串在其第零字符处结束;

char dest[1020];
dest[0] = 0;

Or you could initialize your whole array with 0's

或者你可以用0初始化整个数组

char dest[1024] = {};

And since your question is tagged C++ I cannot but note that in C++ we use std::strings which save you from a lot of headache. Operator + can be used to concatenate two std::strings

由于您的问题被标记为c++,我不得不注意,在c++中,我们使用std::string,它使您避免了很多麻烦。操作符+可用于连接两个std::string

#2


4  

Don't use char[]. If you write:

不要使用char[]。如果你写:

std::string dest;
std::string source( "baby" )
//  ...
dest += source;

, you'll have no problems. (In fact, your problem is due to the fact that strcat requires a '\0' terminated string as its first argument, and you're giving it random data. Which is undefined behavior.)

你不会有问题的。(实际上,您的问题是由于strcat需要一个'\0'终止字符串作为它的第一个参数,并且您给它随机的数据。未定义的行为。)

#3


1  

your dest array isn't initialized. so strcat tries to append source to the end of dest wich is determined by a trailing '\0' character, but it's undefined where an uninitialized array might end... (if it does at all...)

你的数组没有初始化。所以strcat尝试将源代码追加到最重要的位置,这是由一个尾随的“\0”字符决定的,但是它没有定义一个未初始化的数组可能会结束……(如果有的话……)

so you end up printing more or less random characters until accidentially a '\0' character occurs...

所以你最终会打印出或多或少的随机字符,直到偶然地出现一个“\0”字符……

#4


0  

Try this

试试这个

#include <iostream>

using namespace std;

int main()
{
    char dest[1020];
    memset (dest, 0, sizeof(dest));
    char source[7] = "baby";
    cout << "Source: " << source << endl;
    cout << "return value: " << strcat_s(dest, source) << endl;
    cout << "pointer pass: " << dest << endl;
    getchar();
    return 0;
}

Did using VS 2010 Express. clear memory using memset as soon as you declare dest, it's more secure. Also if you are using VC++, use strcat_s() instead of strcat().

使用VS 2010 Express。一旦声明了dest,使用memset清除内存,更安全。如果您正在使用vc++,那么使用strcat_s()而不是strcat()。