100 likes | 436 Views
EENG212 ALGORITHMS & DATA STRUCTURES. Structures in C. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions typedef. Structures. Collections of related variables (aggregates) under one name
E N D
EENG212 ALGORITHMS & DATA STRUCTURES Structures in C EASTERN MEDITERRANEAN UNIVERSITY
Outline • Introduction • Structure Definitions • Initializing Structures • Accessing Members of Structures • Using Structures with Functions • typedef
Structures • Collections of related variables (aggregates) under one name • Can contain variables of different data types • Commonly used to define records to be stored in files • Combined with pointers, can create linked lists, stacks, queues, and trees
Structure Definitions • Example struct card { char *face; char *suit; }; • struct introduces the definition for structure card • card is the structure name and is used to declare variables of the structure type • card contains two members of type char * • These members are face and suit
Structure Definitions • struct information • A struct cannot contain an instance of itself • Can contain a member that is a pointer to the same structure type • A structure definition does not reserve space in memory • Instead creates a new data type used to declare structure variables • Declarations • Declared like other variables: card oneCard, deck[ 52 ], *cPtr; • Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;
Structure Definitions • Valid Operations • Assigning a structure to a structure of the same type • Taking the address (&) of a structure • Accessing the members of a structure • Using the sizeof operator to determine the size of a structure
Initializing Structures • Initializer lists • Example: card oneCard = { "Three", "Hearts" }; • Assignment statements • Example: card threeHearts = oneCard; • Could also declare and initialize threeHearts as follows: card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”;
Accessing Members of Structures • Accessing structure members • Dot operator (.) used with structure variables card myCard; printf( "%s", myCard.suit ); • Arrow operator (->) used with pointers to structure variables card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); • myCardPtr->suit is equivalent to ( *myCardPtr ).suit
Using Structures With Functions • Passing structures to functions • Pass entire structure • Or, pass individual members • Both pass call by value • To pass structures call-by-reference • Pass its address • Pass reference to it • To pass arrays call-by-value • Create a structure with the array as a member • Pass the structure
typedef • typedef • Creates synonyms (aliases) for previously defined data types • Use typedef to create shorter type names • Example: typedef struct Card *CardPtr; • Defines a new type name CardPtr as a synonym for type struct Card * • typedef does not create a new data type • Only creates an alias