title: How can I implement strcpy() and sorting of a struct in C?
body: <p>I am making a small ANSI C application using GCC in Ubuntu which uses strcpy() and sorting.</p>
  
  <p>My header:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define DECKSZ 52
  
  typedef struct card {
      enum {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
      enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
      char cardName[20];
  } card;
  
  card deck[DECKSZ];
  
  void initDeck(card[]);
  void labelCards();
  void shuffleDeck(card[]);
  void swap(card*,card*);
  </code></pre>
  
  <p>My main file:</p>
  
  <pre><code>#include "CardOps.h"
  
  void initDeck(card deck[]) {
      int counter;
      for (counter = 0; counter &lt; DECKSZ; counter++) {
          deck[counter].pips = (const)((counter % 13) + 1);
          deck[counter].suit = (const)(counter / 13);
      }
  }
  
  void labelCards() {
      char pips[13][6] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
      char suits[4][9] = {"Spades","Hearts","Diamonds","Clubs"};
      int i;
      card cardName;
      for (i = 0; i &lt; DECKSZ; i++) {
          strcpy(cardName, pips[i]);
          strcpy(cardName, suits[i]);
      }
  }
  
  int displayCards(int numCards) {
      int i, countCards;
      if (numCards &gt; 52)
          countCards = 52;
      else
          countCards = numCards;
      for (i = 0; i &lt; countCards; i++) {
          printf(cardName);
      }
      return countCards;
  }
  
  void shuffleDeck(card deck[]) {
      int i, j;
      for (i = 0; i &lt; DECKSZ; i++) {
          j = rand() % DECKSZ;
          swap(&amp;deck[i], &amp;deck[j]);
      }
  }
  
  void SortCards() {
  
  }
  
  void swap(card *c1, card *c2) {
      card temp;
      temp = *c1;
      *c1 = *c2;
      *c2 = temp;
  }
  
  int main(void) {
      initDeck(deck);
      shuffleDeck(deck);
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I am trying to implement the following functionality:</p>
  
  <p>A function called LabelCards() that takes as an argument a void and
  returns a void.  Use each of the two enumeration type members in the 
  deck to assign a string to cardName, i.e. “Queen of Hearts”.  (You’ll 
  want to create an array of strings “Ace”, “Two”, “Three”, etc. for the 
  pips and a similar array for the suits to handle the string processing.) 
  Note that you’ll need to use strcpy() to make the actual assignment 
  to cardName.</p>
  
  <p>A function called DisplayCards() that takes as an argument an 
  integer N, and returns an integer. The function should display the 
  cardName of first the N cards in the deck.  The function returns the 
  number of cards displayed, which could be less than the actual deck 
  size.  For example, 53 cards cannot be displayed in a deck of 52; I
  need to check for this and return the actual number of cards 
  displayed.</p>
  
  <p>A function called SortDeck() that takes and returns a void.  My
  function should use the swap() algorithm to organize the cards in 
  the deck first by their pips, and then according to their suit value.</p>
  
  <p>Would somebody please help me get the functionality working? Thanks!</p>