enum [enumeration_tag] {<list of identifiers>} <variables>;
typdef enum {ben=7,paul=1,andrew=-9,kevin=5} StudentsType;
StudentsType Class;
struct date {
short Day,Month;
int Year;
int YearDay;
char MonthName[10];
} Today,Tommorrow,Doomsday;
This definition declares a type struct date which is a structure, and variables of that
type.
typedef struct {
short Day,Month;
int Year;
int YearDay;
char MonthName[10];
} DateType;
DateType Today,Tommorrow,Doomsday;
typedef struct {
char Name[64],Address[128];
int Postcode;
double Salary;
DateType Birthdate;
} AustraliaCardType;
AustraliaCardType BobHawke,PaulKeating;
Tommorrow.Year = 1989; MonthEnd = Today.Month != Tommorrow.Month;
Tommorrow = Today;
DateType Armagedon(AustraliaCardType Instigator);
. . . .
Doomsday = Armagedon(BobHawke);
DateType Tommorow = {28,10,1987,301,"Oct"};
#define POPULATION 10000
typedef struct {
short Day,Month;
int Year;
int YearDay;
char MonthName[4];
} DateType;
typedef struct {
char Name[64],Address[128];
int Postcode;
double Salary;
DateType Birthdate;
} AustraliaCardType;
typedef AustraliaCardType DataBankType[POPULATION];
DataBankType BigBrother;
DataBankType BigBrother =
{{"Adam Ant",
"10 Hill St, Blues City",
1984,
2001.0,
{29,2,1945,60,"Feb"}},
{"Brian Bury",
etc
}}};
As with arrays, the innner {} may be left out and the array will be initialised
in order.
Not good style.
typedef struct {
char Name[10];
long PhoneNumber;
} PhoneBookEntryType;
PhonebookEntryType PhoneBook[] =
{{"Fred",2726547},
{"Mary",3107654},
etc
}};
int NumberOfFriends = sizeof(PhoneBook)/sizeof(PhoneBookEntryType);
Here's an abstracted way of doing that ...
#define LENGTH(A) (sizeof(A)/sizeof(A[0])) int NumberOfFriends = LENGTH(PhoneBook);
typedef union {
int I1;
float F1;
char *S1;
} StrangeType;
StrangeType StrangeVariable;
Write an interactive program that allows the user to place items (as above) into, or remove items from, a queue.
x = 10101010 (binary)
y = 10100111 (binary)
setbits n = 3, p = 6 gives x = 10111010 (binary)
x = 10101010 (binary)
x inverted = 01010101 (binary)
x = 10100111 (binary)
x rotated by 3 = 11110100 (binary)
typedef struct {
char keyword[10];
int other_data;
} Record;
typedef struct {
int Counter;
float Average;
} hit_rate_type;
typdef struct {
char *Name;
hit_rate_type Killer;
} murder_type;
murder_type *JackTheRipper;
write code that mallocs memory for JackTheRipper,
stores the name "Jack the Ripper" in the Name field, and
stores 99 in the Counter.
typedef union {
int WholeThing;
struct {
unsigned Top:8;
unsigned Middle:4;
unsigned Bottom:4;
} Parts;
} int_parts_type;
int_parts_type MyParts;
MyParts.WholeThing = 0x1BB2;
printf("%d\n",MyParts.Parts.Middle);