简体   繁体   English

我如何初始化动态分配的字符数组,所以我不会得到那些奇怪的字符?

[英]how can i initialize a dynamically allocated array of characters so i don't get those weird characters?

I want to use that array of characters in my game of "hangman" to view to tha user his current progress. 我想在我的“hangman”游戏中使用那个字符数组来查看用户当前的进度。

#include <iostream>
#include "randword.h"
#include <fstream>
#include <time.h>
#include <cstdlib>

using namespace std;



int main()
{
InitDictionary();
string tixaio=Randomword();
int m = tixaio.length();
int guesses=8;
char *charptr= new char[m];





for(int aa=0;aa<m;aa++){
charptr[aa]='-';

}

cout << "The word now looks like this: "<<charptr;


}

As soon as i try to cout<< charptr my array i got the usual "---------" plus some weird characters. 一旦我尝试cout << charptr我的数组我得到了通常的“---------”加上一些奇怪的字符。 How can i prevent those characters from showing up?? 我该如何防止这些角色出现?

Strings in C and C++ must end in a "\\0" character: a character that is the value 0. C和C ++中的字符串必须以“\\ 0”字符结尾:一个值为0的字符。

However, you should be using std::string instead of an array of characters. 但是,您应该使用std::string而不是字符数组。 Then you won't have to worry about ending in a null character. 然后你不必担心以空字符结尾。

When you print an array of characters in C++, cout (or any ofstream object, for that matter) looks for the null terminating character to determine when to stop printing. 当您在C ++中打印字符数组时,cout(或任何流对象)会查找空终止字符以确定何时停止打印。 Now, if you've only allocated m bytes for the array, and cout tries to access memory outside of those m bytes, you could possibly even get a segmentation fault. 现在,如果你只为数组分配了m个字节,并且cout尝试访问那些m个字节之外的内存,你甚至可能会遇到分段错误。

One way to avoid this is to allocate space for one more character than what's needed, and assign '\\0' to that extra character. 避免这种情况的一种方法是为一个或多个字符分配空间,并为该额外字符分配'\\ 0'。

char *charptr= new char[m+1];
charptr[m] = '\0';

Since you're using C++, it has a built in std::string class which you can use for your purpose. 由于您使用的是C ++,因此它具有内置的std :: string类,您可以将其用于您的目的。 It automatically appends the null terminating character to the string. 它会自动将空终止字符附加到字符串。 This behavior can be verified by calling the c_str() function on an object of type std::string (this converts std::string to char*). 可以通过在类型为std :: string的对象上调用c_str()函数来验证此行为(这会将std :: string转换为char *)。 It'll always return n+1 characters, where n is the number returned by std::string's length() function. 它总是返回n+1字符,其中n是std :: string的length()函数返回的数字。 And as you can guess, the last character will be \\0. 正如你猜测的那样,最后一个字符将是\\ 0。

它是一个char *,AFAIK,你需要在末尾有一个\\ 0终止符,以便打印知道何时停止。

clear the array to 0 将数组清除为0

memset (array, 0, size);

Or after you make the string, append a '\\0' just after the end of the string, which will indicate the end of string. 或者在创建字符串之后,在字符串结尾之后追加一个'\\0' ,这将指示字符串的结尾。 This will prevent the printing routing go beyond the limit which you want. 这将阻止打印路由超出您想要的限制。 In your case you should place charptr[aa] = '\\0' just after the for loop. 在你的情况下,你应该在for循环之后放置charptr[aa] = '\\0' Make sure that you have enough allocated memory block to charptr you need to allocate the length of string + 1 (for the '\\0' character). 确保你有足够的分配内存块来charptr你需要分配字符串+ 1的长度(对于'\\0'字符)。

你必须在末尾插入'\\ 0'字符,因为它是一个以空字符结尾的字符串

You should add a \\0 as the last element after you fill up the array. 填充数组后,应该添加\\0作为最后一个元素。

Better option is to use std:;string that is the C++ way of doing things better and in a elegant manner, without having to bother about null terminating as in case of char* 更好的选择是使用std:;string ,它是C ++以更好的方式处理事务的方式,而不必像char*那样烦扰null终止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用一些参数初始化动态分配数组中的对象 - How can I initialize objects in a dynamically allocated array with some arguments 如何在类构造函数中初始化动态分配的数组 - How do I initialize a dynamically allocated array within a class constructor 为什么需要在分配时使用()初始化动态分配的数组? - Why do I need to initialize dynamically allocated array at allocation time with ()? 如何创建静态分配的动态大小的数组? - How can I create a statically allocated dynamically sized array? 在 Visual Studio 2019 C++ 中,如何扩展动态分配的数组以显示其所有元素? - In Visual Studio 2019 C++, how can I expand a dynamically allocated array so that all of its elements are displayed? 如果我不先初始化向量,如何使用它? - How can I work with vectors if I don't initialize it first? 如果我不释放/删除动态分配的数组会发生什么? - What happens if i don't free/delete dynamically allocated arrays? 不知道为什么不能在C++中初始化一个数组 - I don't know why I can't initialize an array of an array in C++ 如何存储编译时动态分配的内存以便可以在运行时使用? - How do I store the compile-time dynamically allocated memory so that it can be used in run-time? 如何修复我的代码,以免收到 C6385 警告? - How can I fix my code so I don't get a C6385 warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM