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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8229476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:13:06  来源:igfitidea点击:

how to print char array in c++

c++arrayspointerschar

提问by Carlitos Overflow

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

我如何打印一个字符数组,这样我初始化然后连接到另一个字符数组?请看下面的代码

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

回答by Armen Tsirunyan

You haven't initialized dest

你还没有初始化 dest

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

You were just lucky that it so happened that the 6th (random) value in destwas 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.

您很幸运,碰巧中的第 6 个(随机)值dest0. 如果是第 1000 个字符,则返回值会更长。如果它大于 1024,那么你会得到未定义的行为。

Strings as chararrays 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;

作为char数组的字符串必须用 分隔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::strings 使您免于头痛。运算符 + 可用于连接两个std::strings

回答by James Kanze

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 strcatrequires a '\0'terminated string as its first argument, and you're giving it random data. Which is undefined behavior.)

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

回答by xmoex

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

您的dest数组未初始化。所以strcat尝试附加sourcedestwich的末尾由尾随'\0'字符确定,但未定义未初始化的数组可能在哪里结束......(如果确实如此......)

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

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

回答by getakanda

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 memsetas 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()。