简体   繁体   中英

strcpy() is not copying properly c++

Recently I made a program, it has a character array board[8][8][2];

It is basically meant to be a 8X8 board which can store '2' lettered strings. 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"

Therefore P[j].sym is basically a two letter string and board[1][j] should also be a two letter string.

But the output for

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

is given as P1P2P3P4P5P6P7

and the output for

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

is given as P0P1P2P3P4P5P6P7

For

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

P5P6P7 is the output.

To remove any doubt the whole board[8][8][[2] is already initialised and all of P[j].sym are already initialised.

If it helps here is the code for the initialisation of 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.

But not when I'm using strcpy() What is the problem here. 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' ) . So, even if you need to store just two characters in your string, you must have an extra space to store the Null character.

A character array which does not terminate with a Null character can lead to a lot of other problems. 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...

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 ). So, it continues to do so until the Null character is encountered. But if there is no Null character, then you will most probably get Undefined Behavior. In your case, you just got lucky.

I will show you a brief working of strcpy() ( from here )

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 . 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. So, if there is no Null character at the end, it will give you undefined Behavior.

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.

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.

By looking at the manual pages for strcpy :

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

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++. When you pass in an character array to printf it stops printing at the null character. 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. You need to make your char arrays at least 1 character longer than the string you want to store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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