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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

records

Status
Not open for further replies.
Dec 5, 2004
3
US
hi i hope you guys can help me as my life depends on it :(

I have an array of records and have to access one of these records and change a fields value. How would I go about writing the code for this?

eg i have a structure already defined and one of the fields is called score, how would i access this field in the record and assign a set of values, i need to add 9 values to this record as the person has 9 scores. Incrementing the pointer would move it down to the next element, but i need to input this data all in the same record.

Is it possible to assign more than one value in a field as i need to input 9 values for each person,all in one field!

i`m so confused:(

i`ve been using this syntax.. ptr->score

 
If you really must use a single field to represent multiple values, you could make the field one of the unsigned types, assign each value a bit and go from there.

e.g.

/* A represents bit 1, H represents bit 8 */
enum values { A,B,C,D,E,F,G,H };

struct person {
unsigned score;
...
};

Then, some macros for setting, clearing and testing a value.

#define SET_VALUE(N,B) (N)|=(1<<(B))
#define CLEAR_VALUE(N,B) (N) &= ~(1<<(B))
#define TEST_VALUE(N,B) (((N) & 1<<((B)-1)) > 0 ? 1 : 0)

Usage:

struct person p={0}; /* All values initially clear */

SET_VALUE(p.score,A); /* Set value A */
SET_VALUE(p.score,C); /* Set value C */
CLEAR_VALUE(p.score,A); /* Clear value A */

if (TEST_VALUE(p.score,B))
/* Value B is set in score */
else
/* Value B is not set in score */




Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top