DOC PREVIEW
IUPUI CSCI 23000 - Structures Declarations

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1IntroductionStructure DefinitionsDeclaration of Variables of StructureSlide 5Accessing Members of StructuresStructuresMemory Layoutsizeof OperatorDale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230StructuresDeclarationsDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] RobertsIntroductionIntroductionStructuresStructuresA collection of one or more variables, possibly of different types, A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.grouped together under a single name for convenient handling.Commonly used to define records to be stored in filesCommonly used to define records to be stored in filesCombined with pointers, can create linked lists, stacks, queues, and Combined with pointers, can create linked lists, stacks, queues, and treestreesExample:Example:struct card {struct card { char *face;char *face; char *suit;char *suit; }; }; structstruct introduces the definition for structure card introduces the definition for structure card cardcard is the structure name and is used to declare variables of the is the structure name and is used to declare variables of the structure type structure type cardcard contains two members of type contains two members of type char *char *These members are These members are faceface and and suitsuitDale RobertsStructure DefinitionsStructure DefinitionsExampleExample: : A date consists of several parts, such as the day, month, and year, and the day of the A date consists of several parts, such as the day, month, and year, and the day of the year, and the month nameyear, and the month namestruct date {struct date {int day;int day;int month;int month;int year;int year;int year_date;int year_date;char month_name[4];char month_name[4];};}; datedate: the name of the structure, called : the name of the structure, called structure tagstructure tag.. dayday, , monthmonth, …: the elements or variables mentioned in a structure , …: the elements or variables mentioned in a structure are called are called membersmembers..structstruct information informationA A structstruct cannot contain an instance of itself cannot contain an instance of itselfCan contain a member that is a pointer to the same structure typeCan contain a member that is a pointer to the same structure typeA structure definition does not reserve space in memory A structure definition does not reserve space in memory Instead creates a new data type used to declare structure variablesInstead creates a new data type used to declare structure variablesDale RobertsDeclaration of Variables of StructureDeclaration of Variables of StructureDeclarationsDeclarationsmethod 1method 1::declared like other variables: declare tag first, and then declared like other variables: declare tag first, and then declare variable.declare variable.struct card {struct card {char *face;char *face;char *suit;char *suit;};};struct card oneCard, deck[ 52 ],struct card oneCard, deck[ 52 ], *cPtr;*cPtr;method 2method 2::A list of variables can be declared after the right brace and A list of variables can be declared after the right brace and use comma separated list:use comma separated list:struct card {struct card {char *face;char *face;char *suit;char *suit;} oneCard, deck[ 52 ], *cPtr;} oneCard, deck[ 52 ], *cPtr;method 3method 3::Declare only variables.Declare only variables.struct {struct {char *face;char *face;char *suit;char *suit;} oneCard, deck[ 52 ], *cPtr;} oneCard, deck[ 52 ], *cPtr;struct date {.. .. ..};struct date d1, d2, d3, d4, d5;struct date { .. .. ..} d1, d2, d3;struct date d4, d5;struct { .. .. ..} d1, d2, d3, d4, d5;Dale RobertsStructure DefinitionsStructure DefinitionsValid OperationsValid OperationsAssigning a structure to a structure of the same type Assigning a structure to a structure of the same type Taking the address (Taking the address (&&) of a structure ) of a structure Accessing the members of a structure Accessing the members of a structure Using the Using the sizeofsizeof operator to determine the size of a structure operator to determine the size of a structureInitialization of StructuresInitialization of StructuresInitializer listsInitializer listsExampleExample:: struct card oneCard = { "Three", "Hearts" };struct card oneCard = { "Three", "Hearts" };ExampleExample::struct date d1 = {4, 7, 1776, 186, “Jul”};struct date d1 = {4, 7, 1776, 186, “Jul”};struct date d2 = {4, 7, 1776, 186, {‘J’,’u’,’l’,’\0’}};struct date d2 = {4, 7, 1776, 186, {‘J’,’u’,’l’,’\0’}};Assignment statementsAssignment statementsExampleExample::struct card threeHearts = oneCard;struct card threeHearts = oneCard;Dale RobertsAccessing Members of StructuresAccessing Members of StructuresAccessing structure membersAccessing structure membersDot (Dot (..) is a member operator used with structure ) is a member operator used with structure variablesvariablesSyntax: Syntax: structure_name.memberstructure_name.member struct card myCard;struct card myCard; printf( "%s", myCard.suit );printf( "%s", myCard.suit );One could also declare and initialize One could also declare and initialize threeHeartsthreeHearts as follows: as follows:struct card threeHearts;struct card threeHearts;threeHearts.face = “Three”;threeHearts.face = “Three”;threeHearts.suit = “Hearts”;threeHearts.suit = “Hearts”;Arrow operator (Arrow operator (->->) used with pointers to structure ) used with pointers to structure variablesvariablesstruct card *myCardPtr = &myCard;struct card *myCardPtr = &myCard;printf( "%s", myCardPtr->suit );printf( "%s", myCardPtr->suit ); myCardPtr->suitmyCardPtr->suit is equivalent to is equivalent to (*myCardPtr).suit(*myCardPtr).suitDale RobertsStructuresStructuresStructure can be nestedStructure can be nestedstruct date {struct date {int day;int day;int month;int month;int year;int year;int year_date;int year_date;char month_name[4];char month_name[4];};};struct person {struct person {char name [NAME_LEN];char name [NAME_LEN];char address[ADDR_LEN};char address[ADDR_LEN};long zipcode;long zipcode;long ss__number;long ss__number;double salary;double salary;struct date birthday;struct date birthday;};};struct person emp;struct person emp;emp.birthday.month = 6;emp.birthday.month = 6;emp.birthday.year =


View Full Document

IUPUI CSCI 23000 - Structures Declarations

Download Structures Declarations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Structures Declarations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Structures Declarations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?