1 / 9

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Department of Computer and Information Science, School of Science, IUPUI. CSCI 230. Structures Declarations. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Introduction. Structures

ozzie
Download Presentation

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI CSCI 230 Structures Declarations Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. 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 *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

  3. 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. • 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. struct date { • .. .. .. • }; • struct date d1, d2, d3, d4, d5; struct date { .. .. .. } d1, d2, d3; struct date d4, d5; struct { .. .. .. } d1, d2, d3, d4, d5; 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; 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;

  5. 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" }; 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. Accessing Members of Structures • Accessing structure members • Dot (.) is a member operator used with structure variables • Syntax: structure_name.member struct card myCard; 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

  7. 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. 0 integer 1 2 - 10 9 character 11 (hole) 12 integer 13 0 - 3 integer 4 - 12 9 character 13 - 15 (hole) 16-19 integer 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 day1 int 2 bytes month char array 9 bytes (hole) 1 bytes year int 2 bytes • Quad (4 bytes) address alignment – begins (aligns) at quad address, such as VAX 8200 day1 int 4 bytes month char array 9 bytes (hole) 3 bytes year int 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.)

  9. 5 bytes 1 byte (hole) 2 bytes 1 byte 1 byte (hole) t1 992 997 998 1000 1001 t2 1002 5 bytes 1 byte (hole) 2 bytes 1 byte 1 byte (hole) 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

More Related