简体   繁体   English

strcpy()无法正确复制c ++

[英]strcpy() is not copying properly c++

Recently I made a program, it has a character array board[8][8][2]; 最近我做了一个程序,它有一个字符数组board[8][8][2];

It is basically meant to be a 8X8 board which can store '2' lettered strings. 它基本上是指可以存储“ 2”个字母字符串的8X8板。 I am not providing the complete code. 我没有提供完整的代码。

But here is the problem. 但这是问题所在。

for (j = 0; j < 8; j++) {
    strcpy(board[1][j], P[j].sym);
}

cout << board[1][1] << endl;

Here P[1].sym="P1" and P[0].sym="P0" and P[2].sym="P2" 此处P[1].sym="P1"P[0].sym="P0"P[2].sym="P2"

Therefore P[j].sym is basically a two letter string and board[1][j] should also be a two letter string. 因此, P[j].sym基本上是两个字母的字符串, board[1][j]也应该是两个字母的字符串。

But the output for 但是输出为

cout << board[1][1] << endl;

is given as P1P2P3P4P5P6P7 给出为P1P2P3P4P5P6P7

and the output for 和输出为

cout << board[1][0] << endl;

is given as P0P1P2P3P4P5P6P7 给出为P0P1P2P3P4P5P6P7

For 对于

cout << board[1][5] << endl;

P5P6P7 is the output. P5P6P7是输出。

To remove any doubt the whole board[8][8][[2] is already initialised and all of P[j].sym are already initialised. 为了消除任何疑问,整个board[8][8][[2]已被初始化,所有P[j].sym已被初始化。

If it helps here is the code for the initialisation of P : 如果有帮助,这里是P初始化的代码:

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

class Game
{
public:
   char board[8][8][2];
   char *****possibilities;
 };

class Pawn : virtual public Game {
 public:
     char sym[2];
     int possiblec[4][2];
     Pawn() { }

     Pawn(int i) {
         char a[2];
         a[0] = 'P';
         a[1] = (char)(i + 48);
         strcpy(sym, a);
     }
};

And here somewhere else in the program I did 在程序的其他地方

    Pawn P[8];

It calls the constructor and then later on I called the parameterised contructor explicitly. 它调用构造函数,然后稍后我显式调用参数化构造函数。

for (int i = 0; i < 8; i++) {
    P[i] = i;
}

After this I checked for different values of P[j].sym and all of them return the perfect values I wanted. 在此之后,我检查了P[j].sym不同值,它们全部返回了我想要的理想值。

But not when I'm using strcpy() What is the problem here. 但是,当我使用strcpy()时没有,这是什么问题。 This program is just a practice program to get a hang of it. 该程序只是一个实践程序,可以帮助您一臂之力。

Character arrays in C++ ( and C ) are terminated with a Null character ( '\\0' ) . C ++(和C)中的字符数组以Null字符( '\\0' )结尾。 So, even if you need to store just two characters in your string, you must have an extra space to store the Null character. 因此,即使您只需要在字符串中存储两个字符,也必须有一个额外的空间来存储Null字符。

A character array which does not terminate with a Null character can lead to a lot of other problems. 不以Null字符结尾的字符数组会导致很多其他问题。 It is a wrong practice. 这是错误的做法。

If your character array does not terminate with a Null character, you will get a lot of problems when you call functions such as strcpy() , strcat() , etc... 如果您的字符数组不以Null字符结尾,则在调用诸如strcpy()strcat()等函数时会遇到很多问题。

So, you should change 所以,你应该改变

char board[8][8][2]

to

char board[8][8][3]

And if you have any other strings just like this one, then leave one extra space in them as well. 而且,如果您还有其他类似的字符串,那么也要在其中留出一个额外的空间。

The reason your code behaved as such is because you got lucky. 您的代码如此行事的原因是因为您很幸运。

Functions such as strcpy() , strcat() all continue to copy ( or append ) until they encounter a Null Character ( which is numerically equal to zero ). 诸如strcpy()strcat()函数都将继续复制(或追加),直到遇到Null字符(数值上等于零)为止。 So, it continues to do so until the Null character is encountered. 因此,它将继续这样做,直到遇到Null字符为止。 But if there is no Null character, then you will most probably get Undefined Behavior. 但是,如果没有Null字符,那么您很可能会得到未定义的行为。 In your case, you just got lucky. 就您而言,您很幸运。

I will show you a brief working of strcpy() ( from here ) 我将向您展示strcpy()的简要工作(从这里开始

char * strcpy(char p, const char * q) {
while (*p++=*q++);
//there's also a return p; statement at the end
}

That is the function. 那就是功能。

the while loop executes until it encounters false, and the equivalent for false is 0 . while循环一直执行到遇到false为止,而false等于0 So, when it encounters a Null character ( which is also numerically equal to 0 ), the while loop terminates and the copying is complete, and the function ends. 因此,当它遇到一个Null字符(在数值上也等于0 )时,while循环终止,复制完成,该函数结束。 So, if there is no Null character at the end, it will give you undefined Behavior. 因此,如果末尾没有Null字符,它将为您提供不确定的行为。

You can refer man for more info about them 您可以向男人推荐有关它们的更多信息

You should always reserve one extra character because strings in C and C++ are null terminated , which that they need one extra character to sign the end of the string. 您应该始终保留一个额外的字符,因为C和C ++中的字符串以null terminated ,因此它们需要一个额外的字符来对字符串的结尾进行签名。

So, please, change 所以,请改变

board[8][8][2]

to

board[8][8][3]

as well as sym[2] to sym[3] , a[2] to a[3] (generally add one to the length of all strings) and try again. 以及sym[2]sym[3]a[2]a[3] (通常在所有字符串的长度上加一个),然后重试。

By looking at the manual pages for strcpy : 通过查看strcpy的手册页:

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). 将source指向的C字符串复制到destination指向的数组中,包括终止的空字符(并在该位置停止)。

This means that that function will stop only when it encounters the null character. 这意味着该函数仅在遇到空字符时才会停止。 That's why it would fail if there wasn't any present. 这就是为什么如果没有礼物会失败的原因。 But, by setting one character at a time, there's obviously no such problem visible (it will become visible later on, if you try to execute a function that stops only when it encounters a null character and there are plenty of them). 但是,通过一次设置一个字符,显然不会出现这样的问题(稍后,如果您尝试执行仅在遇到空字符且有很多空字符时才停止的函数,该问题将变得可见)。

Strings are null ('\\0') terminated in C++. 在C ++中终止的字符串为null('\\ 0')。 When you pass in an character array to printf it stops printing at the null character. 当您将字符数组传递给printf时,它将在空字符处停止打印。 I'm guessing the only reason it stopped printing at P7 is because you got lucky and the next memory location happens to be storing Null. 我猜它在P7停止打印的唯一原因是因为您很幸运,下一个内存位置恰好存储了Null。 You need to make your char arrays at least 1 character longer than the string you want to store. 您需要使char数组至少比要存储的字符串长1个字符。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM