Solved

Arrays of Structure

Posted on 1998-10-30
5
217 Views
Last Modified: 2010-04-02
When I assign the values to arrays of structure, how can I print them?  Or what wrong with the following my C-progam?

/* Arrays of Stuctures */
#include <stdio.h>

struct card {
      char *face;
      char *suit;
};


main()
{
      int i, j;
      struct card a[13];
      char *face[] = {"Ace", "Deuce", "Three", "Four", "Five",
                              "Six", "Seven", "Eight", "Nine", "Ten",
                              "Jack", "Queen", "King"};
      char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};

      for (i=0; i<=12; i++){
         printf("%6s", a[i].face);
      }
      printf("\n");
      for (j=0; j<=3; j++){
         printf("%8s", a[j].suit);
      }
      printf("\n");
      return 0;
}
0
Comment
Question by:mkido
5 Comments
 

Accepted Solution

by:
vmehro earned 50 total points
Comment Utility
char *face[] = {"Ace", "Deuce", "Three", "Four", "Five",
   "Six", "Seven", "Eight", "Nine", "Ten",
   "Jack", "Queen", "King"};
????
You gotta initialize the member "face" of the structure a[]
like
a[0] = {"Ace", "Hearts"}, etc
0
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
or you could make face and suit integers in your structure (to be used as indices)

struct card {
int face;
int suit;
}

and print it out the fields like this:

for (i=0; i<=12; i++){
   printf("%6s", face[a[i].face]);

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>You gotta initialize the member "face" of the structure a[]
>>   like
>>   a[0] = {"Ace", "Hearts"}, etc

That will not compile.  The "{ }" syntax may only be used during construction.  The "a[0] ="  is after constuction.

Try

struct card a[13] = {{"Ace","Hearts"},
   {"Duece","Hearts"},
   {"Thre","Hearts"},
   {"Four","Hearts"},
   {"Five","Hearts"},
   {"Six","Hearts"},
   {"Seven","Hearts"},
   {"Eight","Hearts"},
   {"Nine","Hearts"},
   {"Ten","Hearts"},
   {"Jack","Hearts"},
   {"Queen","Hearts"},
   {"King","Hearts"}};
0
 

Author Comment

by:mkido
Comment Utility
If I want to avoid the following long initialization, how do you do?  Thanks.
struct card a[52] = {{"Ace","Hearts"},
         {"Duece","Hearts"},
         {"Thre","Hearts"},
         {"Four","Hearts"},
         {"Five","Hearts"},
         {"Six","Hearts"},
         {"Seven","Hearts"},
         {"Eight","Hearts"},
         {"Nine","Hearts"},
         {"Ten","Hearts"},
         {"Jack","Hearts"},
         {"Queen","Hearts"},
         {"King","Hearts"}},
         {"Ace","Diamonds"},
         {"Duece","Diamonds"},
         {"Thre","Diamonds"},
         {"Four","Diamonds"},
         {"Five","Diamonds"},
         {"Six","Diamonds"},
         {"Seven","Diamonds"},
         {"Eight","Diamonds"},
         {"Nine","Diamonds"},
         {"Ten","Diamonds"},
         {"Jack","Diamonds"},
         {"Queen","Diamonds"},
         {"King","Diamonds"},
         {"Ace","Clubs"},
         {"Duece","Clubs"},
         {"Thre","Clubs"},
         {"Four","Clubs"},
         {"Five","Clubs"},
         {"Six","Clubs"},
         {"Seven","Clubs"},
         {"Eight","Clubs"},
         {"Nine","Clubs"},
         {"Ten","Clubs"},
         {"Jack","Clubs"},
         {"Queen","Clubs"},
         {"King","Clubs"}},
         {"Ace","Spades"},
         {"Duece","Spades"},
         {"Thre","Spades"},
         {"Four","Spades"},
         {"Five","Spades"},
         {"Six","Spades"},
         {"Seven","Spades"},
         {"Eight","Spades"},
         {"Nine","Spades"},
         {"Ten","Spades"},
         {"Jack","Spades"},
         {"Queen","Spades"},
         {"King","Spades"}};


0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You could use a loop to initialize the card array from an array of suites and array of face names, like

char *Suits[4] = {"Clubs","Hearts","Spafes",Diamonds"};
char *Faces[13]  = {"Ace","Deuce","Two",Three","Four","Five","Six",
    "Seven","Eight","Nine","Ten","Jack","Queen","King"};
card a[52];

int SuitNum = 0;
int FaceNum = 0;
for (int i = 0; i < 52; ++i)
{
   a[i].suit = Suites[SuiteNum];
   a[i].face = Faces[FaceNum];
   ++FaceNum;
   if (FaceNum >= 13)
   {
     ++SuiteNum;
     FaceNum = 0;
   }
}


Note that if you use this approach, the string values in the card array point to string that are used multiple times.  These string should not be changed.  (Although the pointers can be).  To make this safe, I would declare the card structure as having "const char *" pointers not just "char *" pointers.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now