开发者

Passing char array in a function?

Here is my problematic coding: I have to take in 2 player's name. Then when for the next part when the player marker changes the name stored in "currentPlayer" should change too the name stored in either playerOne or playerTwo. It doesn't so how do I fix that? Please solve, I tried to make it a reference variable with the & symbol but I get an error saying array of reference is not allowed.

void boardMarker(int &, char playerOne[], char playerTwo[], char &playerMarker, char currentPlayer[]);

int main()

{
    char playerOne[100];
    char playerTwo[100];
    char currentPlayer[100] = "playername";

    boardMarker(playerTurn, playerOne, playerTwo, playerMarker, currentPlayer);

}
void boardMarker(int &playerTurn, char playerOne[100], char playerTwo[100], char &playerMarker, char currentPlayer[100])
{
    // Set 开发者_如何学Goplayer markers
    //Player 1 uses X and Player 2 uses O
    if ( playerTurn == 1 )
    {
        playerMarker = 'X';
        currentPlayer = playerOne;
    }
    else
    {
        playerMarker = 'O';
        currentPlayer = playerTwo;
    }
}


  • You can't assign arrays to one another (you have to copy them element by element)
  • When passed to functions, arrays decay to pointers, so as an argument, char playerOne[100] is identical to char* playerOne
  • Assigning a char* to another char* does not copy the string, it copies the pointer.

The correct way to do this:

currentPlayer = playerOne;

is this:

strcpy(currentPlayer, playerOne);

Or, better yet, since this is C++ and not C, use std::string instead of char arrays. std::string will behave essentially how you expect.


You are copying array pointers instead of the values in them.

Read a C tutorial on arrays http://augustcouncil.com/~tgibson/tutorial/arr.html


You want currentPlayer to be a pointer-to-characters, then swap it between the two players:

Your code, edited:

void boardMarker(int&, char playerOne[], char playerTwo[], char &playerMarker, char** pCurrentPlayer);

int main()
{
    char playerOne[100];
    char playerTwo[100];
    char* currentPlayer = playerOne;

    boardMarker(playerTurn, playerOne, playerTwo, playerMarker, &currentPlayer);

}
void boardMarker(int &playerTurn, char playerOne[100], char playerTwo[100], char &playerMarker, char** pCurrentPlayer)
{
    // Set player markers
    //Player 1 uses X and Player 2 uses O
    if ( playerTurn == 1 )
    {
        playerMarker = 'X';
        *pCurrentPlayer = playerOne;
    }
    else
    {
        playerMarker = 'O';
        *pCurrentPlayer = playerTwo;
    }
}

Some comments on your code:

  • variables playerTurn and playerMarker are not declared. (I'm pretending they are global variables, not shown here).
  • You shouldn't leave parameters un-named, such as the dangling int& in the prototype of boardMarker.
  • As written, playerOne and playerTwo are uninitialized. Let's pretend they're initialized elsewhere.


In C/C++ the name of an array is actually a pointer to the first element of the array. So for example

*currentPlayer == 'p'

Now, when you're passing an array into a function like that, you are making a copy of the pointer, but not the array itself.

So when in your function you say

currentPlayer = playerOne

All you are doing is making the pointer currentPlayer point to the same memory location as the pointer playerOne.

To get what you want, you need to use strcpy

strcpy(currentPlayer,playerOne)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜