Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.

Similar presentations


Presentation on theme: "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7."— Presentation transcript:

1 Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7

2 Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous storage elements all of the same data type. struct The abstract data type struct avoids one restriction: NEED NOT The elements be NEED NOT all be of the same data type. Structs (structured data objects) struct A struct : Consists of 2 or more component data types A component part may be either a simple data type intunsigned intfloatcharlongdouble (e.g., int, unsigned int, float, char, long, double, etc.) A pointer intfloatcharlongdouble (e.g., * int, * float, * char, * long, * double, etc.) Or another structured object struct (i.e., abstract type struct)

3 Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs struct A struct corresponds to records in a database Assume we maintain a simple database of student information Attribute (Field) Data Type Student NameString (Allow for 30 characters) Student IDInteger (int) MajorString (Allow for 10 characters) Grade-Point Average float Real (float) Hours CompletedInteger (int) student This record (let’s call it student ): Consists of 5 (five) data elements Consists of 3 (three) different data types (Student Name, Student ID, Major, Grade-Point Average, Hours Completed) float (Strings (2), Integers (2), and a real (float))

4 Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Declaring Records (in other languages): COBOL: 01STUDENT 02 STUDENT-NAMEPIC X(30). 02 STUDENT-IDPIC 9(6). 02 MAJORPIC X(10). 02 GPAPIC 9V99. 02 TOTAL-HOURSPIC 999. Pascal : typestudent = record student_name : array[1..30] of char; student_id : integer; major: array[1..10] of char; gpa: real;. total_hours : integer; end; SQL : create table student ( student_name char(30) not null, student_id integer not null, major char(10) default ‘CIS’, gpa decimal(4,2), total_hours smallint, primary key (student_id), check (gpa > 0) )

5 Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Declaring Records (in C): struct student { char student_name[31]; // Remember: we need 1-byte for ‘\0’ int student_id; char major[11]; // Remember: we need 1-byte for ‘\0’ float gpa; int total_hrs; }; struct student We have created a new data type: struct student struct student just as with basic data types, we can now associate variables (specific locations in RAM) with the data type struct student struct student just as with basic data types, whenever we declare a variable to be of type struct student, we are requesting a specific number of contiguous bytes of RAM storage. How many bytes of storage do we need for a struct? That depends on the struct

6 Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs For our struct (struct student): ElementBytes char char student_name[31];31 int int student_id;2 char char major[11];11 float float gpa;4 int int total_hrs;2 50 50 struct student We need 50 bytes of contiguous RAM storage for our data type struct student How do we declare and use structs in a C program ??

7 Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Consider the following program: #include structstudent { char student_name[31]; int student_id; char major[11]; float gpa; int total_hrs; }; int main() { struct student active = {"Favre, Brett",12345,"CIS",3.27,67}; struct student alumni = {"White, Reggie", 23456, "Pain", 1.34, 89}; printf("Name: %13s %14s\n",active.student_name, alumni.student_name); printf("ID: %13d %14d\n",active.student_id, alumni.student_id); printf("Major: %13s %14s\n",active.major, alumni.major); printf("gpa: %13.3f %14.3f\n",active.gpa, alumni.gpa); printf("Hours: %13d %14d\n",active.total_hrs, alumni.total_hrs); return 0;}

8 Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs How is this data stored in RAM?? struct As with ALL data types, the data type struct has a base address: activestruct student assume that variable active (of data type struct student) has the base address 7520 (and requiring 50 contiguous bytes, to 7569) 7520 F 7521 a 7522 v 7523 r 7524 e 7525, 75267527 B 7528 r 7529 e 7530 t 7531 t 7532 \0 7533 -- 7534 -- 7535 -- 7536 -- 7537 -- 7538 -- 7539 -- 7540 -- 7541 -- 7542 -- 7543 -- 7544 -- 7545 -- 7546 -- 7547 -- 7548 -- 7549 -- 7550 -- 75647561 -- 7552 12345 7553 C 7554 I 7555 S 7556 \0 7557 -- 7558 -- 7559 -- 7560 -- 7562 -- 7563 -- 75517565 756875677566 3.27 7569 67

9 Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs How can the address of each element in the struct be calculated?? struct Just as we calculated the address of each element in an array, the address of each element in a struct is determined by adding the amount of storage required for each element and adding it to the base address. ElementStart addr.StorageNext Address student_name752031-bytes 7520 + 31 = 7551 student_ID75512-bytes 7551 + 2 = 7553 major Or: 7520 + 33 = 7553 755311-bytes 7553 + 11 = 7564 gpa75644-bytes Or: 7520 + 44 = 7564 7564 + 4 = 7568 Or: 7520 + 48 = 7568 total_hrs 75682-bytes N/A Type char [31] int char [11] float int

10 Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs BUT, if structs are equivalent to records in a database, how can a database consist of only one (1) record ??? structint floatchar We can have an array of struct, just as we can have an array of int, and array of float, an array of char (string), etc. struct Remember: a struct, while abstract, is simply a data type struct student { char student_name[20]; // names no longer than 19 characters float gpa; }; For the sake of simplicity, let’s assume the following record structure: How would this struct be set up as an array ???

11 Page 11 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Consider the following code: #include struct student { char student_name[20]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt", 2.05}, {"Rice, Jerry", 3.55}}; int i; for (i = 0; i < 5; i++) printf("Name: %15s gpa: %4.2f\n", active[i].student_name,active[i].gpa); return 0; }

12 Page 12 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs The program, as entered, produces the output : Name: Favre, Brett gpa: 3.26 Name: White, Reggie gpa: 1.34 Name: Smith, Bruce gpa: 3.78 Name: Smith, Emmitt gpa: 2.05 Name: Rice, Jerry gpa: 3.55 How does this program work ?? What if we were searching for a specific name, …. Say, Bruce Smith ??? Consider the following C code

13 Page 13 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs #include struct student { char student_name[20]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt",2.05}, {"Rice, Jerry",3.55}}; int i = 0; char find[20] = "Smith, Bruce"; while((strcmp(active[i].student_name,find)!=0)&&(i<5)) i++; if (i >= 5) printf("The name is NOT on the list"); else printf("%s IS on the list (gpa = %4.2f)", active[i].student_name, active[i].gpa); return 0; }

14 Page 14 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs structs structs and pointers structs structs, like any other data type, have base addresses, and can be accessed by referring to its address. Consider the following modification of our previous program: #include struct student { char name[18]; float gpa; }; int main() { struct student active[5] = {{"Favre, Brett",3.26}, {"White, Reggie",1.34}, {"Smith, Bruce",3.78}, {"Smith, Emmitt",2.05}, {"Rice, Jerry",3.55}}; struct student *record = active; printf(“%lu\n”, &record); while(record <= &active[4]) { printf(“%lu %20s %7.3f\n”, record, record->name, record->gpa); record++; { return 0; }

15 Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs The following output would be produced: 86712 86562Favre, Brett3.260 86584White, Reggie1.340 86606Smith, Bruce3.780 86628Smith, Emmitt2.050 86650Rice, Jerry3.550 Note: These addresses are not the TRUE addresses, and they may change with each run How does this program work ??

16 Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Let’s take it from the line: struct student * record = active; Our new data type struct student { char name[18]; float gpa; }; pointer to BASE The BASE address array of our arrayactive &active[0] The same as: &active[0] record active 86562 Meaning, the variable pointer record is now initialized to the BASE address of array active ( or, in or case, 86562) How would this appear in RAM??

17 Page 17 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs 86562 F 86563 a 86564 v 86565 r 86566 e 86567, 8656886569 B 86570 r 86571 e 86572 t 86573 t 86574 \0 86577 -- 86578 -- 86579 -- 86581 3.26 8658286583 86584 W 86586 h i 86587 t 86589, 86593 g 86595 i 8660386596 e record active&active[0] The contents of address record ( = active OR &active[0]) Points to 86575 -- 86576 -- 865808659086591 R 86592 e 86594 g 86597 \0 86598 -- 86599 -- 86588 e 8660286604 1.34 86605 Continue to 86650 R 86651 i 86652 c 86653 e 86654, 8665586656 J 86657 e 86658 r 86659 r 86660 y 86661 \0 86662 -- 86663 -- 86664 -- 86665 -- 86666 -- 86600 -- 86601 -- 86667 -- 8666886669 3.55 8667086671 86712 86713 86562 8671486715 Located at

18 Page 18 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs The next statement in the program: printf(“%lu\n”, &record); Prints the BASE ADDRESS of our structure 86712 86713 86562 8671486715 NOT NOT the contents active (Which contains the base address of our array active) record Notice also that ALL pointers (e.g., record) require 4-bytes of storage We can now begin our loop: while(record <= &active[4]) record record CONTAINS the address 86712 86713 86562 8671486715 Which is less than the base address of our 5th record (86650) Enter Loop

19 Page 19 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Now we simply print out the first record: printf(“%lu %20s %7.3f\n”, record, record->name, record->gpa); record The CONTENTS of record name Field: name (on 20 spaces) gpa Field: gpa (as f7.3) 86562Favre, Brett3.260 (*record).name An alternative statement is: (*record).name record->name Notice that the statement record->name is a REDIRECT: Go to the ADDRESS contained in record and find field name recordname i.e., record contains the address 86562, and name is at 86562 record->gpa The redirect record->gpa would yield the address 86580

20 Page 20 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs The next statement:record++; record Increments the contents of record: 86712 86713 86562 8671486715 Changed to: 86712 86713 86584 8671486715 record The contents of record are increased by 22 Why are the contents increased by 22 and not 1 ??? record --;--record; Record is pointer to our data type struct student (Initialized as: struct student *record;) which requires 22-bytes of storage. Incrementing increases the value contained by 22. Decrementing it (i.e., record --; or --record; would decrease the value contained by 22)

21 Page 21 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Remember: int *integer_pointer;// contains an address to an integer float *float_pointer;// contains an address to a float double *double_pointer;// contains an address to a double IF:VariableLocated at Contains integer_pointer1234546788 float_pointer1234922734 double_pointer1235371122 A pointer is an address in RAM which contains an address What type of data is at that address MUST be known in advance Then:StatementWill result in the new value integer_pointer++; 46790 ++float_pointer;22738 double_pointer--;71114

22 Page 22 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs For the rest of the loop: record record value Output 86584White, Reggie1.340 86584 86606Smith, Bruce3.780 86606 86628Smith, Emmitt2.050 86628 86650Rice, Jerry3.550 86650 86672 Greater than Stop

23 Page 23 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs


Download ppt "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7."

Similar presentations


Ads by Google