0

I'm trying to write a C code to represent a deck of cards by first creating the struct for a card, and then copying the strings from 2D arrays into the suits and ranks of those cards.

The program doesn't return any faults, but when I tried to display the cards the character are all mixed up or just the first character got displayed. How could I fix this?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char ranks[13][10] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
              "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

char suits[4][10] = {"Clubs", "Diamonds", "Hearts", "Spades"};

typedef struct{
    char rank;
    char suit;
    int value;
}card;

void initialize(card *s){
    //s = (person*) malloc(1 * sizeof( person));
     for (int i = 0; i < 52; i++){
        strcpy(&(s+i)->rank , ranks[i % 13]);
        strcpy(&s[i].suit , suits[i / 13]);
        (s+i)->value = i % 13 +1;
    }

}

int main(){
    card *p; 
    
    initialize(p);

    for (int i = 0; i < 52 ; i++){
        printf("%s is %s\n", &p[i].rank, &p[i].suit);
    }

    return 0;
}

the output is sth like this

AClu is Clu
TClu is Clu
TClu is Clu
FClu is Clu
FClu is Clu
SClu is Clu
SClu is Clu
ECl is Clu
NClu     is Clu
TClu
 is Clu

JClu
     is Clu 
2
  • pass the address of p and make changes as *s inside initialize Commented Jan 11, 2021 at 9:58
  • struct members are char not char [] you cant strcpy to it Commented Jan 11, 2021 at 10:00

1 Answer 1

1

Your structure card have only one elements for storing strings. This isn't enough for storing strings with any positive length (because terminating null-character is required).

You should change the members rank and suit to arrays and allocate enough elements. Also remove &s because then they will be automatically converted to pointers to the first elements.

Also don't forget to allocate some buffer and assign that to p.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char ranks[13][10] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
              "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
char suits[4][10] = {"Clubs", "Diamonds", "Hearts", "Spades"};
typedef struct{
    char rank[10];
    char suit[10];
    int value;
}card;

void initialize(card *s){
    //s = (person*) malloc(1 * sizeof( person));
     for (int i = 0; i < 52; i++){
        strcpy((s+i)->rank , ranks[i % 13]);
        strcpy(s[i].suit , suits[i / 13]);
        (s+i)->value = i % 13 +1;
    }

}
int main(){
    card data[52];
    card *p = data;
    
    initialize(p);

   
    for (int i = 0; i < 52 ; i++){
        printf("%s is %s\n", p[i].rank, p[i].suit);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I’d remove the unnecessary p in the code, it’ll just confuse OP. You’ve also copied OP’s odd choice of sometimes using (s + i)-> and sometimes s[i]. without explaining that they’re the same. And you’ve added a sneaky memory allocation for the card deck without explaining the need for it.
@KonradRudolph My opinion is directly changing p to an array instead of allocating separate array is better than removing p, which is in the original code.

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.