1 / 27

INC 161 , CPE 100 Computer Programming

INC 161 , CPE 100 Computer Programming. Lecture 13 Structure. How large data are organized?. array Group of data, same type structure Group of data, different types class Group of data and functions. Structure Definition.

jeramy
Download Presentation

INC 161 , CPE 100 Computer Programming

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. INC 161 , CPE 100Computer Programming Lecture 13 Structure

  2. How large data are organized? array Group of data, same type structure Group of data, different types class Group of data and functions

  3. Structure Definition A structure is a collection of members that can be of different data types. Example: struct Student { int id; char name[32]; }; • The keyword struct introduces the definition for structure Student. Student is the structure name (or tag of the structure) and is used to declare variables of the structure type • Student contains two members of type int and array of char.The two members id and name are used to hold the student id and student name.

  4. Memory Storage for struct Student struct Student { int id; char name[32]; }; Structure name = Student Members = 2 (1 integer + 1 array)

  5. Declaration of Structure type The syntax of a structure declaration can be fairly complex. The common method to declare structures are presented below. • Declaring a tag name and then using the tag name to declare actual variables. Example: struct Student { int id; char name[32]; }; ... struct Student s; s is a variable of struct Student type with two members id and name.

  6. Declaring a structure without using a tag name. struct { int id; char name[32]; } s; This is useful if the structure is used only in one place. • Declaring a structure with a tag name and variables. struct Student { int id; char name[32]; } s1, s2; Boths1 and s2 are variables of struct Student type.

  7. Pointer to Structures Declaring a pointer to structure is the same as declaring pointers to other objects. struct Student { int id; char name[32]; } *sp1; struct Student *sp2, **spp3; Both sp1 and sp2 are pointers to struct Student type. spp3 is a pointer to pointer to struct Student.

  8. Accessing Structure Members There are two methods to access structure members. The first method is to access a structure member by structure name. Method 1.(Dot Operator) Using structure variable name, dot operator (.) and the name of the member field. struct Student s; s.id = 101;

  9. /* File: accessmember.c */ #include <stdio.h> struct Student { int id; char name[32]; }; main() { struct Student s; printf(“Please input id and name\n”); scanf(“%d”, &s.id); scanf(“%s”, s.name); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); } > acccessmember.c Please input id and name 102 Doe s.id = 102 s.name = Doe Example: (Accessing structure members by structure name)

  10. Accessing Structure Members (Cont.) Method 2. (Arrow Operator) The second method to access a structure member is through a pointer to the structure. Using a pointer to a structure, the arrow operator (->) and the name of a member field. struct Student s, *sp; sp = &s; ... printf("sp->id = %d\n", sp->id); The symbol “sp->id” is equivalent to (*sp).id

  11. /* File: spointer.c */ #include <stdio.h> struct Student { int id; char name[32]; }; main() { struct Student s, *sp; sp = &s; printf(“Please input id and name\n”); scanf(“%d”, &sp->id); scanf(“%s”, sp->name); printf("sp->id = %d\n", sp->id); printf("sp->name = %s\n", sp->name); } > spointer.c Please input id and name 102 Smith sp->id = 102 sp->name = Smith Example: (Accessing structure members through a pointer to the structure)

  12. Typedef for Structure Type By using typedef, we can omit the “struct” keyword. struct Student { int id; char name[32]; }; ... typedef struct Student student_t; ... student_t s; // struct Student s; or typedef struct { int id; char name[32]; } student_t; ... student_t s; typedef makes student_t equivalent to struct student

  13. Typedef for Structure Type(Cont.). typedef struct Student { int id; char name[32]; } student_t, *studentptr_t; ... student_t s; // struct Student s; studentptr_t sptr; // struct Stuednt *sptr; student_t is the type of struct Student. studentptr_t is the type of pointer to struct Student.

  14. /* File: typedefs.c */ #include <stdio.h> #include <string.h> typedef struct Student { int id; char name[32]; } student_t; main() { student_t s; s.id = 101; strcpy(s.name, "John"); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); } Output: s.id = 101 s.name = John

  15. Structure Initialization Two common ways to initialize the structures. 1) struct Student { int id; char name[32]; } s={101, "John"}; 2) struct Student s = {102, "Doe"}; Following example uses above two methods to initialize two variables of struct Student type and print out the results. Variable s is a global variable.

  16. /* File: initial.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s={101, "John"}; main() { struct Student s2 = {102, "Doe"}; printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); printf("s2.id = %d\n", s2.id); printf("s2.name = %s\n", s2.name); } Example: Output: s.id = 101 s.name = John s2.id = 102 s2.name = Doe

  17. Size of Structures The size of a structure can be calculated by sizeof() operator. The argument of operator sizeof() can be the struct type or the variable of struct type. The example below prints out the size of struct Student. /* File: sizeofs.c */ #include <stdio.h> struct Student { int id; char name[32]; } s, *sp; main() { printf("sizeof(struct Student) = %u\n", sizeof(struct Student)); printf("sizeof(s) = %u\n", sizeof(s)); printf("sizeof(struct Student*) = %u\n", sizeof(struct Student*)); printf("sizeof(sp) = %u\n", sizeof(sp)); } Output: sizeof(struct Student) = 36 sizeof(s) = 36 sizeof(struct Student*) = 4 sizeof(sp) = 4

  18. Assigning and Comparing Structures The assignment operator ‘=‘ can be applied to both structure and pointer to structure. A structure can be assigned to a structure variable, provided that they are of the same structure type. The value for each member of the structure of the rvalue will be assigned to the corresponding member of the lvalue. The relational operators ‘= =‘ and ‘!=“ can be applied to variables of pointer to structure type, not to variables of structure type. To compare variables of structure type, compare their corresponding member fields. Output: s.id = 101 s.name = John sp and sp1 point to different objects. The contents of s and s1 are the same.

  19. Assigning and Comparing Structures /* File: assign.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s1 = {101, "John"}; main() { struct Student s, *sp, *sp1; s = s1;// structure assignment printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); sp = &s; sp1 = &s1; if(sp == sp1) printf("sp and sp1 point to the same object.\n"); else printf("sp and sp1 point to different objects.\n"); if(s.id == s1.id && !strcmp(s.name, s1.name)) printf("The contents of s and s1 are the same.\n"); }

  20. Array of Structures Because a structure is a data object, it is possible to create arrays of structure. Each element of the array has structure data type. An array of structure can declared by the same method for declaring a structure variable. Following example declares and initializes an array of structure with three elements. The values of elements of the array are also printed out.

  21. /* File: arrays.c */ #include <stdio.h> struct Student { int id; char name[32]; }; main() { struct Student s[3] = {{101, "John"}, {102, "Doe"}, {103, “Peter"}}; int i, num; printf(“Number of elements is %d\n”, num); for(i=0; i<3; i++) { printf("s[%d].id = %d\n", i, s[i].id); printf("s[%d].name = %s\n", i, s[i].name); } } Output: Number of elements is 3 s[0].id = 101 s[0].name = John s[1].id = 102 s[1].name = Doe s[2].id = 103 s[2].name = Peter

  22. Memory layout and contents of structure array s

  23. /* File: arraysp.c */ #include <stdio.h> struct Student { int id; char name[32]; }; main() { struct Student *sp, s[3] = {{101, "John"}, {102, "Doe"}, {103, "Peter"}}; int i; sp = s; for(i=0; i<3; i++) { printf("sp[%d].id = %d\n", i, sp[i].id); printf("sp[%d].name = %s\n", i, sp[i].name); } for(i=0; i<3; i++, sp++) { printf("id = %d\n", sp->id); printf("name = %s\n", sp->name); } } Example • Pointer sp is treated as array in the for-loop. • Pointer sp is advanced to the next element by expression sp++ in the while-loop. Output: sp[0].id = 101 sp[0].name = John sp[1].id = 102 sp[1].name = Doe sp[2].id = 103 sp[2].name = Peter id = 101 name = John id = 102 name = Doe id = 103 name = Peter

  24. Nested Structures It is called a nested structure when one of the members of a structure is a structure also. For example, struct Student is a nested structure because the member birthday is a structure with struct Birthday type. struct Birthday { int day, month, year; }; struct Student { int id; char name[32]; struct Birthday birthday; } s, *sp; The field day, month, and year can be accessed by the following format. s.birthday.day sp->birthday.day s.birthday.month sp->birthday.month s.birthday.year sp->birthday.year

  25. /* File: nested.c */ #include <stdio.h> #include <string.h> struct Birthday { int day, month, year; }; struct Student { int id; char name[32]; struct Birthday birthday; }; main() { struct Student s, *sp; sp = &s; s.id = 101; strcpy(s.name, "John"); /* September 16, 1990 */ s.birthday.day = 16; s.birthday.month = 8; sp->birthday.year = 1990; // s.birthday.year = 1990; printf("id = %d\n", s.id); printf("name = %s\n", s.name); printf("birthday = %d\n", s.birthday.day); printf("birthd month = %d\n", s.birthday.month); printf("birth year = %d\n", sp->birthday.year); } Output: id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example

  26. Nested Structures The struct Student is a nested structure. The member birthday of struct Student is a pointer to structure Birthday. struct Birthday { int day, month, year; }; struct Student { int id; char name[32]; struct Birthday *birthday; } s, *sp; The field day, month, and year can be accessed by the following format. s.birthday->day sp->birthday->day s.birthday->month sp->birthday->month s.birthday->year sp->birthday->year

  27. /* File: nestedpointer.c */ #include <stdio.h> #include <string.h> struct Birthday { int day, month, year; }; struct Student{ int id; char name[32]; struct Birthday *birthday; }; main() { struct Student s, *sp; struct Birthday bday; • sp = &s; s.id = 101; strcpy(s.name, "John"); s.birthday = &bday; s.birthday->day = 16; s.birthday->month = 8; s.birthday->year = 1990; printf("id = %d\n", s.id); printf("name = %s\n", s.name); printf("birthday = %d\n", s.birthday->day); printf("birth month = %d\n", s.birthday->month); printf("birth year = %d\n", s.birthday->year); } Output: id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example

More Related