tags:

views:

99

answers:

4

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 player markers
    //Player 1 uses X and Player 2 uses O
    if ( playerTurn == 1 )
    {
        playerMarker = 'X';
        currentPlayer = playerOne;
    }
    else
    {
        playerMarker = 'O';
        currentPlayer = playerTwo;
    }
}
+5  A: 
  • 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.

Tyler McHenry
+1  A: 

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

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

Akusete
A: 

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)
Matt Edlefsen
Arrays are not pointers, I won't downvote as it a common misunderstanding but you should review both your knowledge and the answer. The name of the array **is** an identifier that refers to the array, the confusion comes from the fact that arrays have a tendency to *decay* into pointers in most expressions and the fact that the language allows the array syntax in a few places where it is parsed as pointer syntax (i.e. a function parameter `int a[10]` is parsed as `int*`. But arrays are still arrays in the scope where they are defined and can be used as such in places where pointers cannot.
David Rodríguez - dribeas
An example would be `void foo( int (`. That function takes an array of exactly 3 ints by reference. `int main() { int array[3]; int *p = array; foo(array); /*foo(p);*/ }`. `array` can be passed to `foo` as *it is* an array of 3 integers, `p` cannot because it is a pointer to an integer (that happens to be initialized to the beginning of an array of 3 integers.
David Rodríguez - dribeas
Saying that arrays are pointers is a common way of explaining them to people who may be new to C because it makes a lot of their "strange" behavior make more sense.I probably should have been more clear that arrays work like pointers, but are not exactly the same.BTW, I'm not sure I've ever written code like in your example. I did know that sizeof(array) was different though.
Matt Edlefsen
+1  A: 

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.
abelenky
This will give the effect he wants, but changes the semantics of his program. `currentPlayer` is now an alias to one of the player name strings, rather than holding the value of the current player name.
Tyler McHenry