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;
typedef struct { char Name[20]; int Type:4; signed int Class:3; unsigned int UsedYet:1; int NumberOfUses; } SymbolType; SymbolType SymbolDetails;
typedef struct { unsigned Ready:1; unsigned ErrorOccured:1; unsigned DiskSpinning:1; unsigned WriteProtect:1; unsigned HeadLoaded:1; unsigned ErrorCode:8; unsigned Track:9; unsigned Sector:5; unsigned Command:5; } DiskRegisterType;
DiskRegisterType *DiskRegister = (DiskRegisterType *) DiskRegisterMemory;
//----Define sector and track to start read DiskRegister->Sector = NewSector; DiskRegister->Track = NewTrack; DiskRegister->Command = READ; //----Wait until operation done, ready will be true while (! DiskRegister->Ready) { } //----Check for errors if (DiskRegister->ErrorOccured) { //----Interrogate error_code for error type switch (DiskRegister->ErrorCode) { .... } }
unsigned int *DiskRegister = (unsigned int *) DiskRegisterMemory; //----See if disk error occured DiskErrorOccured = (DiskRegister & 0x40000000) >> 31;
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);