Solved

# Arrays of Structure

Posted on 1998-10-30
Medium Priority
223 Views
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
Question by:mkido
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Accepted Solution

vmehro earned 150 total points
ID: 1176641
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

ID: 1176642
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

ID: 1176643
>>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

ID: 1176644
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"}},

0

LVL 22

Expert Comment

ID: 1176645
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month12 days, 6 hours left to enroll