Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struct help

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
How can I declare an array (containing 5 element) of structures that contain two fields – name and score. Then initialize the array, print one structure per line after initialization, and pass the array to another function to find and print the highest score and the corresponding name.

The function takes as arguments a pointer to the structure and the size of the array
 
struct sRec
{
char strName[35];
int iScore;
};

struct sRec asRec[5];

my_init(asRec,5); /* do your home work here */
my_print(asRec,5);/* do your home work here */
my_max_score(asRec,5);/* do your home work here */


 
You could do like this:

...
struct TStudent
{
char *name;
int score;
};

TStudent studentArray[MAX];

// to initialize

studentArray[0].score = 60;

// function call

myFunction(studentArray,MAX); Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
About aphrodita declaration.
in C you can not skip "struct" in variable declaration.
You can do it by typedef

typedef struct
{
.....
}TStudent;
TStudent studentArray[MAX];

(you can do it in C++)
 
About aphrodita declaration.
in C you can not skip "struct" in variable declaration.
You can do it by typedef

typedef struct
{
.....
}TStudent;
TStudent studentArray[MAX];

(you can do it in C++)
 
As declared by others.....
#define MAX 5
struct User
{
int Score;
char Name[10];
};

Initialise the array structure...............
struct User Table[MAX] =
{ /*Score Name */
{150, "name1" },
{50, "name2" },
{5, "name3" },
{20, "name4" },
{20, "name5"}
};

Using the array structure............
To find the maximum score.
int Counter;
int HighestScore;
int HighestScore;
for(HighestScore=0, Counter=0; Counter<MAX; Counter)
{
if (User[Counter].Score > HighestScore) HighestScore = User[Counter].Score;
}

You can use the array structure in a similiar way to print the name. I am guessing name is an array of chars.
User[HighestScore].Name;
Have fun.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top