1

I have a function and I want to modify cardString[] that is passed to it so that cardString[] contains [charSuit, cardRank, '\0']:

void getCard(int row, int column, char cardString[]){
    PlayingCard myCard = myDeck.cardArray[row][column];
    char charSuit;
    char charRank;   

    if (myCard.getSuit() == CLUB)
        charSuit = 'C';
    else if (myCard.getSuit() == SPADE)
        charSuit = 'S';
    else if (myCard.getSuit() == HEART)
        charSuit = 'H';
    else if (myCard.getSuit() == DIAMOND)
        charSuit = 'D';
    if (myCard.getRank() == NINE)
        charRank = '9';
    else if (myCard.getRank() == TEN)
        charRank = 'T';
    else if (myCard.getRank() == JACK)
        charRank = 'J';
    else if (myCard.getRank() == QUEEN)
        charRank = 'Q';
    else if (myCard.getRank() == KING)
        charRank = 'K';
    else if (myCard.getRank() == ACE)
        charRank = 'A';
}
6
  • I want to modify the char array that is passed to the function. Commented Mar 6, 2012 at 1:58
  • The char array is originally defined as charArray[3]=['\0','\0','\0'] Commented Mar 6, 2012 at 1:59
  • @user1004358: it's really just a pointer, so you can write to it about like any other (pointer or array syntax). Commented Mar 6, 2012 at 1:59
  • so i add char *p; p=cardString; *p=charSuit+charRank; ? Commented Mar 6, 2012 at 2:00
  • cardString is an array of characters...what do you want to store in it? Commented Mar 6, 2012 at 2:01

1 Answer 1

2

There's no one-line way to assign an entire array; you need to write three assignment statements:

cardString[0] = charSuit;
cardString[1] = charRank;
cardString[2] = '\0';
Sign up to request clarification or add additional context in comments.

2 Comments

but my function is a void function i want to change cardString in memory.
@user1004358: Yes, exactly. That's what the above statements will do. (Are you seeing some sort of problem?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.