Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations."— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations

2 Dale Roberts Introduction Structures A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and trees Example: struct card { char *face; char *face; char *suit; }; char *suit; }; struct introduces the definition for structure card struct introduces the definition for structure card card is the structure name and is used to declare variables of the structure type card is the structure name and is used to declare variables of the structure type card contains two members of type char * card contains two members of type char * These members are face and suit

3 Dale Roberts Structure Definitions Example: A date consists of several parts, such as the day, month, and year, and the day of the year, and the month name struct date { int day; int month; int year; int year_date; char month_name[4]; }; date : the name of the structure, called structure tag. date : the name of the structure, called structure tag. day, month, …: the elements or variables mentioned in a structure are called members. day, month, …: the elements or variables mentioned in a structure are called members. 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

4 Dale Roberts Declaration of Variables of Structure Declarations method 1:declared like other variables: declare tag first, and then declare variable. struct card { char *face; char *suit; }; struct card oneCard, deck[ 52 ], *cPtr; *cPtr; method 2:A list of variables can be declared after the right brace and use comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; method 3:Declare only variables. struct { char *face; char *suit; } 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;

5 Dale Roberts 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 Initialization of Structures Initializer lists Example: struct card oneCard = { "Three", "Hearts" }; struct card oneCard = { "Three", "Hearts" }; Example: struct date d1 = {4, 7, 1776, 186, “Jul”}; struct date d2 = {4, 7, 1776, 186, {‘J’,’u’,’l’,’\0’}}; Assignment statements Example: struct card threeHearts = oneCard;

6 Dale Roberts Accessing Members of Structures Accessing structure members Dot (. ) is a member operator used with structure variables Syntax: structure_name.member struct card myCard; struct card myCard; printf( "%s", myCard.suit ); printf( "%s", myCard.suit ); One could also declare and initialize threeHearts as follows: struct card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”; Arrow operator ( -> ) used with pointers to structure variables struct card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); myCardPtr->suit is equivalent to (*myCardPtr).suit myCardPtr->suit is equivalent to (*myCardPtr).suit

7 Dale Roberts Structures Structure can be nested struct date { int day; int month; int year; int year_date; char month_name[4]; }; struct person { char name [NAME_LEN]; char address[ADDR_LEN}; long zipcode; long ss__number; double salary; struct date birthday; }; struct person emp; emp.birthday.month = 6; emp.birthday.year = 1776; Name Rule Members in different structure can have the same name, since they are at different position. struct s1 {.... char name[10];.... } d1; struct s2 {.... int name;........ } d2; struct s3 {.... int name; struct s2 t3;.... } d3; float name;

8 Dale Roberts Memory Layout Example: struct data1 { int day1; char month[9]; int year; }; Word (2 bytes) alignment machine – begins (aligns) at even address, such as PC, SUN workstation day1int2 bytes monthchar array9 bytes (hole)1 bytes yearint 2 bytes Quad (4 bytes) address alignment – begins (aligns) at quad address, such as VAX 8200 day1int4 bytes monthchar array9 bytes (hole)3 bytes yearint 4 bytes You must take care of hole, if you want to access data from very low level (i.e. low-level I/O, byte operations, etc.) 0 1 2 - 10 11 12 13 integer 9 character (hole) 0 - 3 4 - 12 13 - 15 16-19 integer 9 character (hole)

9 Dale Roberts sizeof Operator sizeof(struct tag) struct test { char name[5]; int i;/* assume int is 2 bytes */ char s; } t1, t2; main(){ printf(“sizeof(struct test) = %d\n”, sizeof (struct test)); printf(“address of t1 = %d\n”, &t1); printf(“address of t2 = %d\n”, &t2); printf(“address of t1.name = %d\n”, t1.name); printf(“address of t1.i = %d\n”, &t1.i); printf(“address of t1.s = %d\n”, &t1.s); }output: sizeof(struct test) = 10 address of t1 = 992 address of t2 = 1002 address of t1.name = 992 address of t1.i = 998 address of t1.s = 1000 2 bytes 5 bytes 1 byte (hole) 1 byte 1 byte (hole) 992 998 1000 2 bytes 5 bytes 1 byte (hole) 1 byte 1 byte (hole) 1002 1001 997 t2 t1


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations."

Similar presentations


Ads by Google