Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing char array in a function?

Tags:

c++

arrays

char

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;
    }
}
like image 783
Renge Avatar asked Jul 30 '10 02:07

Renge


People also ask

Can you pass an array through a function?

C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index.

How do you pass a 2d char array into a function?

You can just pass arrays as function arguments with definition of their size. bool canReach(char board[ROWS][COLS], int i, int j); When the size is unknown, pointers are the way.

How do you pass an array as a pointer to a function in C?

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.


3 Answers

  • 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.

like image 92
Tyler McHenry Avatar answered Oct 21 '22 04:10

Tyler McHenry


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

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

like image 29
Akusete Avatar answered Oct 21 '22 05:10

Akusete


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.
like image 24
abelenky Avatar answered Oct 21 '22 05:10

abelenky