1 / 19

Chapter 10 Structures and Unions

Chapter 10 Structures and Unions. PROGRAMMING IN C. Question. An array can be used to represent a group of data items of the same type. Sometimes, we want to represent a collection of data items of different types, but we can't use arrays. These variables are independent, so it is

fai
Télécharger la présentation

Chapter 10 Structures and Unions

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. Chapter 10Structures and Unions PROGRAMMING IN C

  2. Question • An array can be used to represent a group of data items of the same type. • Sometimes, we want to represent a collection of data items of different types, but we can't use arrays.

  3. These variables are independent, so it is difficult to reflect their internal relation. Question • For example, we want to represent the information of a student, such as: student_number, name, sex, age, entrance_score and address, we can define these like: int num; char name[20]; char sex; int age; float score; char addr[30]; struct student { } ; Structure!

  4. Defining a Structure • The definition of structure is not for a variable but type. • Like the data type provided by system (such as: int, float, double, char, and so on), the definition of structure used to declare variables. • The difference is: the definition of structure is defined by user. • "Structure" is a constructed data type.It is used to pack data of different types. It is a user-defined type. A valid identifier, and can be omitted. struct [tag_name] { data_type member1; data_type member2; …… } ; structis a keyword, and can't be omitted. Can be primary type or derived type.

  5. 2 bytes num … name 20 bytes 1 byte sex 2 bytes age 4 bytes score …… addr 30 bytes Defining a Structure • The definition of structureis the description of the data organization form. It is a description of data type defined by user, not a declaration of variable. Variables in such type organize their elements and require storage in memory according to such form. Each variable requires 59(in TC) bytes storage. struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; };

  6. 2 bytes 2 bytes stu2.num stu1.num stu2.name stu1.name 20 bytes 20 bytes … … 1 byte 1 byte stu2.sex stu1.sex 2 bytes 2 bytes stu2.age stu1.age 4 bytes 4 bytes stu2.score stu1.score …… …… stu2.addr stu1.addr 30 bytes 30 bytes Declaring Structure Variables • There are 3 ways to declare structure variables: • First define structure type, and then declare structure variables. struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1, stu2; struct tag_name { data_type member1; data_type member2; …… } ; struct tag_name variables list;

  7. Declaring Structure Variables • Define structure type, at the same time, declare structure variables. struct tag_name { data_type member1; data_type member2; …… } variables list; struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; } stu1, stu2;

  8. Declaring Structure Variables • Declare structure variables immediately. struct { data_type member1; data_type member2; …… } variables list; struct { int num; char name[20]; char sex; int age; float score; char addr[30]; } stu1, stu2;

  9. birthday num name month day year Declaring Structure Variables • Structure members can also be a structure . struct date { int month; int day; int year; } ; struct student { int num; char name[20]; struct date birthday; } stu; struct student { int num; char name[20]; struct date { int month; int day; int year; } birthday; } stu;

  10. Declaring Structure Variables • A structure member name can be the same as another variable name. main ( ) { struct student { int num; char name[20]; } stu; int num; num = 0; stu.num = 1; }

  11. Accessing Structure Members • Like arrays, we can't read in or output the whole structure variable, and we can't use the whole structure variables to evaluate expression. We must access structure members. • Structure members are not variables, so they should be linked to the structure variables in order to make them meaningful members. • The accessing form:structure variable.member Member operator (dot operator) Precedence: 1 Associativity: left to right

  12. Accessing Structure Members • We can't read in or output the whole structure variable.However, structure variables can be initialized as a whole. A structure variable can be assigned to another. struct student { int num; char name[20]; char sex; } stu1, stu2 = {101, "Jack", 'M'}; stu1 = stu2; scanf ("%d, %s, %c", &stu1.num, stu1.name, &stu1.sex); printf ("%d, %s, %c", stu1.num, stu1.name, stu1.sex);

  13. Accessing Structure Members main() { struct date { int month; int day; int year; }; struct student { int num; char name[20]; struct date birthday; } stu={101, "Jack", {1, 31, 1986}}; printf ("num: %d\nname: %s\nbirthday: %d-%d-%d\n", stu.num, stu.name, stu.birthday.year, stu.birthday.month, stu.birthday.day) ; } num: 101 name: Jack birthday: 1986-1-31

  14. Accessing Structure Members • We can't directly judge whether 2 structure variables are same or not, and we must compare each member. struct student { int num; char name[20]; } stu1, stu2 = {101, "Jack"}; stu1 = stu2; if ( stu1 == stu2 ) printf ("OK!"); struct student { int num; char name[20]; } stu1, stu2 = {101, "Jack"}; stu1 = stu2; if(stu1.num==stu2.num && !strcmp(stu2.name, stu1.name)) printf ("OK!"); Error … : Illegal structure operation in function ...

  15. num name 25B stu[0] sex age num name stu[1] sex age Arrays of Structure • In an array of structure, each element represents a structure variable. struct student { int num; char name[20]; char sex; int age; }; struct student stu[2]; struct student { int num; char name[20]; char sex; int age; } stu[2]; struct { int num; char name[20]; char sex; int age; } stu[2];

  16. Arrays of Structure - Initialization struct student { int num; char name[20]; char sex; } stu1[2] = {101, “John”, ‘M’, 102, “Mary", 'F' }, stu2[2] = { {101, "John", 'M'}, {102, "Mary", 'F'}}, stu3[ ] = { {101, "John", 'M'}, {102, "Mary", 'F'} };

  17. Arrays of Structure - Accessing Members struct student { int num; char name[20]; char sex; } stu[2] = {101, "John", 'M' } ; stu[1].sex = 'F'; strcpy ( stu[1].name, "Mary" ); printf ( "%d", stu1[1].num );

  18. Define a struct stu(num,name,score[3]), assign the values in main(),and output in print()) #include <stdio.h> #include <String.h> struct student{ int num; char name[20]; double score[3]; }; void main() { void print(struct student); struct student stu; stu.num=12345; strcpy(stu.name,"Zhangsan"); stu.score[0]=67.5; stu.score[1]=89; stu.score[2]=78.6; print(stu); } void print(struct student stu) { printf("%d %s %.2f %.2f %.2f",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]); printf("\n"); }

  19. name count count 0 0 leader[0] Jack 0 0 leader[1] Marry 0 0 leader[2] Tom main() { int i, j, n=10; char name[20]; struct person { char name[20]; int count; } leader[3] = { "Jack", 0, "Marry", 0, "Tom", 0 }; for ( i=1; i<=n; i++ ) { scanf ( "%s", name ); for ( j = 0; j < 3; j++ ) if ( strcmp(name, leader[j].name) == 0 ) { leader[j].count++; break; } } for ( j = 0; j < 3; j++ ) printf ( "%s: %d\n", leader[j].name, leader[j].count ); } Jack  Jack  Marry  Jack  Tom  Marry  Jack  Marry  Jack  Jack  Jack : 6 Marry: 3 Tom: 1 Arrays of Structure - Program • Calculate the ballot of each candidate.

More Related