Chapter 10 Structures and Unions

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
1 C++ Syntax and Semantics The Development Process.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Array, Structure and Union
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Computer Programming II
Variables Mr. Crone.
CS1010 Programming Methodology
Chapter 10-1: Structure.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Revision Lecture
TMF1414 Introduction to Programming
Structure.
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Module 2 Arrays and strings – example programs.
Arrays in C.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Lecture 8.
Derived types.
INC 161 , CPE 100 Computer Programming
EKT150 : Computer Programming
Review for Final Exam.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Chapter 1: Introduction to Data Structures(8M)
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review for Final Exam.
Arrays I Handling lists of data.
C Programming Lecture-13 Structures
Structures In C Programming By Rajanikanth B.
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
C Programming Lecture-14 Unions
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Structure (i.e. struct) An structure creates a user defined data type
Variables in C Topics Naming Variables Declaring Variables
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lecture 8.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

Chapter 10 Structures and Unions PROGRAMMING IN ANSI C

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.

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!

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.

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.

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;

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;

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;

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

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; }

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

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);

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: 1986-1-31

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

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];

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'} };

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 );

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.