EENG 212 ALGORITHMS DATA STRUCTURES Structures in C

  • Slides: 10
Download presentation
EENG 212 ALGORITHMS & DATA STRUCTURES Structures in C

EENG 212 ALGORITHMS & DATA STRUCTURES Structures in C

Outline n Introduction n Structure Definitions n Initializing Structures n Accessing Members of Structures

Outline n Introduction n Structure Definitions n Initializing Structures n Accessing Members of Structures n Using Structures with Functions n typedef

Structures n Collections of related variables (aggregates) under one name n Can contain variables

Structures n Collections of related variables (aggregates) under one name n Can contain variables of different data types Commonly used to define records to be stored in files n Combined with pointers, can create linked lists, stacks, queues, and trees n

Structure Definitions n Example struct card { char *face; char *suit; }; struct introduces

Structure Definitions n Example struct card { char *face; char *suit; }; struct introduces the definition for structure card n card is the structure name and is used to declare variables of the structure type n card contains two members of type char * n n These members are face and suit

Structure Definitions n struct information n A struct cannot contain an instance of itself

Structure Definitions n struct information n 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 n Instead creates a new data type used to declare structure variables n Declarations n Declared like other variables: card one. Card, deck[ 52 ], *c. Ptr; n Can use a comma separated list: struct card { char *face; char *suit; } one. Card, deck[ 52 ], *c. Ptr;

Structure Definitions n Valid Operations n Assigning a structure to a structure of the

Structure Definitions n Valid Operations n Assigning a structure to a structure of the same type n Taking the address (&) of a structure Accessing the members of a structure n Using the sizeof operator to determine the size of a structure n

Initializing Structures n Initializer lists n Example: card one. Card = { "Three", "Hearts"

Initializing Structures n Initializer lists n Example: card one. Card = { "Three", "Hearts" }; n Assignment statements n Example: card three. Hearts = one. Card; n Could also declare and initialize three. Hearts as follows: card three. Hearts; three. Hearts. face = “Three”; three. Hearts. suit = “Hearts”;

Accessing Members of Structures n Accessing structure members n Dot operator (. ) used

Accessing Members of Structures n Accessing structure members n Dot operator (. ) used with structure variables card my. Card; printf( "%s", my. Card. suit ); n Arrow operator (->) used with pointers to structure variables card *my. Card. Ptr = &my. Card; printf( "%s", my. Card. Ptr->suit ); n my. Card. Ptr->suit is equivalent to ( *my. Card. Ptr ). suit

Using Structures With Functions n Passing structures to functions n Pass entire structure n

Using Structures With Functions n Passing structures to functions n Pass entire structure n n Or, pass individual members Both pass call by value n To pass structures call-by-reference n Pass its address n Pass reference to it n To pass arrays call-by-value n Create a structure with the array as a member n Pass the structure

typedef n typedef Creates synonyms (aliases) for previously defined data types n Use typedef

typedef n typedef Creates synonyms (aliases) for previously defined data types n Use typedef to create shorter type names n n Example: typedef struct Card *Card. Ptr; Defines a new type name Card. Ptr as a synonym for type struct Card * n typedef does not create a new data type n n Only creates an alias