Download presentation
Presentation is loading. Please wait.
1
Chapter 10 Structures and Unions
PROGRAMMING IN ANSI C
2
2/23/2019 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
2/23/2019 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: struct student { } ; int num; char name[20]; char sex; int age; float score; char addr[30]; These variables are independent, so it is difficult to reflect their internal relation. Structure!
4
2/23/2019 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; …… } ; struct is a keyword, and can't be omitted. Can be primary type or derived type.
5
2/23/2019 Defining a Structure The definition of structure is the description of the data organization form. It is a description of data type defined by user, not a declaration of variable. name num sex age score addr 2 bytes 20 bytes 1 byte 4 bytes 30 bytes … …… struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; Variables in such type organize their elements and require storage in memory according to such form. Each variable requires 59 bytes storage.
6
Declaring Structure Variables
2/23/2019 Declaring Structure Variables stu1.name stu1.num stu1.sex stu1.age stu1.score stu1.addr 2 bytes 20 bytes 1 byte 4 bytes 30 bytes … …… stu2.name stu2.num stu2.sex stu2.age stu2.score stu2.addr There are 3 ways to declare structure variables: First define structure type, and then declare structure variables. struct tag_name { data_type member1; data_type member2; …… } ; struct tag_name variables list; struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1, stu2;
7
Declaring Structure Variables
2/23/2019 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
2/23/2019 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
Declaring Structure Variables
2/23/2019 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; num name birthday month day year
10
Declaring Structure Variables
2/23/2019 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
2/23/2019 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
2/23/2019 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
2/23/2019 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:
14
Accessing Structure Members
2/23/2019 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
Arrays of Structure num name sex age stu[0] stu[1] 25B
2/23/2019 Arrays of Structure num name sex age stu[0] stu[1] 25B In an array of structure, each element represents a structure variable. struct { int num; char name[20]; char sex; int age; } stu[2]; 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];
16
Arrays of Structure - Initialization
2/23/2019 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
2/23/2019 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
Arrays of Structure - Program
2/23/2019 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; } } printf ( "%s: %d\n", leader[j].name, leader[j].count ); Jack Marry Tom Jack : 6 Marry: 3 Tom: 1 Arrays of Structure - Program count leader[0] leader[1] leader[2] name Jack Marry Tom Calculate the ballot of each candidate.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.